Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 12 total  RSS 

Get Frontmost Window

tell application "System Events"
	set frontmostApplication to name of the first process whose frontmost is true
end tell

Mark Selected Message as Read

tell application "Mail" to try
   tell message viewer 1 to set selected messages to {first message of beginning of (get selected mailboxes) whose read status is false}
   activate
on error
   beep
end try

Applescript Export QT Streaming Movie

// description of your code here

 export movie 1 to save_location as hinted movie using default settings

Set Desktop Background droplet

-- This script should be run as a droplet
-- It expects that an image is dropped on it's icon


-- 
-- The on run handler is called if the user has double clicked the icon
--
on run
	display dialog "Please drop an image file on this Icon to set it as the desktop background"
end run


--
-- The on open handler is called when the user drags and drops  afile on the icon
--
on open draggeditems
	set imageFile to last item of draggeditems
	
	tell application "Finder"
		select window of desktop
		try
			set desktop picture to file (imageFile as text)
		on error
			display dialog "Invalid image file."
		end try
	end tell
	
end open



Compile the front TextWrangler java source file using applescript

// If you use TextWrangler to edit java source code,
// this AppleScript will compile the front window.

tell application "TextWrangler"
	set the_file to file of text document 1
end tell

set AppleScript's text item delimiters to ":"
set source_file to the last text item of (the_file as string)

tell application "Finder"
	set the_folder to container of the_file as alias
end tell

tell application "Terminal"
	activate
	set shell_script to "cd " & (quoted form of POSIX path of the_folder) & "; javac " & source_file
	if (count windows) is 0 then
		do script shell_script
	else
		do script shell_script in the front window
	end if
end tell

Quick search with Quicksilver and EasyFind

// A script to quickly search files and folders using Quicksilver and EasyFind.

(*
A script to quickly search files and folders using Quicksilver [1] and EasyFind [2].
Written by Simon Dorfman, January 8, 2006. www.SimonDorfman.com

Save this script to ~/Library/Application Support/Quicksilver/Actions/EasyFind.scpt
You'll need to have "Enable access for assisstive devices" enabled under Universal Access in System Preferences.
To use:
	1. activate Quicksilver
	2. type "." (i.e. hit the period key)
	3. type the text you want to search for (i.e. httpd.conf)
	4. press tab
	5. start typing "EasyFind.scpt"
	6. once Quicksilver finds "EasyFind.scpt", press return
	
[1] http://www.versiontracker.com/dyn/moreinfo/macosx/22549
[2] http://www.versiontracker.com/dyn/moreinfo/macosx/11706
*)

using terms from application "Quicksilver"
	on process text t
		tell application "EasyFind" to activate
		tell application "System Events"
			tell process "EasyFind"
				set value of text field 1 of window 1 to t --type text in search field
				--uncomment your preferred search option, or save separate scripts with each option
				click radio button 1 of radio group 1 of window 1 --"Files & Folders"
				--click radio button 2 of radio group 1 of window 1 --"Only Files"
				--click radio button 3 of radio group 1 of window 1 --"Only Folders"
				--click radio button 4 of radio group 1 of window 1 --"File Contents"
				click button 4 of window 1 --start search
			end tell
		end tell
	end process text
end using terms from

Resize Front Two Windows Side by Side, Filling Screen

// An applescript that resizes the front two windows of the active application side by side, filling the screen.

-- An applescript that resizes the front two windows of the active application side by side, filling the screen.
-- Written by Simon Dorfman, January 4, 2006. www.SimonDorfman.com

-- Save this script to ~/Library/Scripts/Applications/Run from QuickSilver Triggers, Don't move these/Front 2 Windows Side by Side - Control + Option + C.scpt
-- I use keyboard shortcut Control + Option + C to run it.  I think of the C as standing for "Compare".
-- You'll need to have "Enable access for assisstive devices" enabled under Universal Access in System Preferences.

--set screen dimension variables
set menubarHeight to 22
set screenWidth to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
set screenHeight to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number
--if you plan to use this on just one computer where the screen dimensions won't change, this script will run faster if you just hard code your screen resolution with these two lines:
--set screenWidth to 1280
--set screenHeight to 854
{screenWidth, screenHeight}

tell application "System Events"
	set frontApp to name of first application process whose frontmost is true
end tell

--some apps are wacky and put the windows higher for some reason, adjust for this bug.
if (frontApp is equal to "Finder" or frontApp is equal to "Microsoft Entourage") then
	set menubarHeight to 44
end if
--leave room for the Excel Toolbar
if (frontApp is equal to "Microsoft Excel") then
	set menubarHeight to 55
end if

try
	tell application frontApp
		set bounds of window 1 to {0, menubarHeight, (screenWidth / 2), screenHeight}
		set bounds of window 2 to {(screenWidth / 2), menubarHeight, screenWidth, screenHeight}
	end tell
on error the error_message number the error_number
	display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try

Play DVD from Hard Drive Shortcut

// a quick way to play a DVD that has been saved to the hard drive

-- a quick way to play a DVD that has been saved to the hard drive

-- adapted from a script found in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297

-- Instructions for use:
-- 1. Paste this script into Script Editor and save as an application bundle to ~/Library/Scripts/Applications/Finder/Play DVD Folder
-- 2. Control-click and drag it to the top of a finder window so it appears in every finder window.
-- 3. Next, we'll give our script the DVD Player icon to make it's use more intuitive by entering these commands in Terminal:
--     $ cd ~/Library/Scripts/Applications/Finder/Play\ DVD\ Folder.app/Contents/Resources/
--     $ rm droplet.icns
--     $ cp /Applications/DVD\ Player.app/Contents/Resources/app.icns droplet.icns
--     $ touch ~/Library/Scripts/Applications/Finder/Play\ DVD\ Folder.app
-- 4. In the Finder, drag a DVD Folder onto the "Play DVD Folder at the top of that finder window".
-- 5. Enjoy the movie. :-)


-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as string)
		on error
			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	set newWindow to false
	repeat with thePath in theList
		set thePath to thePath as string
		if not (thePath ends with ":") then
			set x to the offset of ":" in (the reverse of every character of thePath) as string
			set thePath to (characters 1 thru -(x) of thePath) as string
		end if
		CD_to(thePath, newWindow)
		set newWindow to true -- create window for any other files/folders
	end repeat
	return
end open

-- open and play the movie
on CD_to(theDir, newWindow)
	try
		tell application "DVD Player"
			activate
			if theDir ends with "VIDEO_TS:" then
				set videoTS to theDir as alias
			else
				set videoTS to theDir & "VIDEO_TS:" as alias
			end if
			open VIDEO_TS videoTS
			set viewer full screen to true
			play dvd
		end tell
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
end CD_to

Open current Finder window folder in TextMate

// Opens the current Finder window folder in TextMate. Or open a file or folder in TextMate by dragging it onto this script.

-- Opens the current finder window folder in TextMate. Or open a file or folder in TextMate by dragging it onto this script.
-- adopted from a script I found in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297

-- Instructions for use:
-- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/open in TextMate
-- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
-- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
-- Activate it by clicking on it or dragging a file or folder onto it.

-- Another nice touch is to give the saved script the same icon as TextMate.
-- To do this, in the finder, Get info (Command-I) of both TextMate and this saved script.
-- Click the TextMate icon (it will highlight blue) and copy it by pressing Comand-C.
-- Click on this script's icon and paste by pressing Command-V.

-- Another way to give it the same icon as TextMate is to save the script as an application bundle (instead of an application),
--  then copy the icon by entering these commands in Terminal:
-- $ cd ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app/Contents/Resources/
-- $ rm droplet.icns
-- $ cp /Applications/TextMate.app/Contents/Resources/TextMate.icns droplet.icns
-- $ touch ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app

-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as string)
		on error
			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	CD_to(theList, false)
end open

-- open the file/folder in TextMate
on CD_to(theDir, newWindow)
	try
		set theDir to quoted form of POSIX path of theDir as string
		do shell script "open -a TextMate " & theDir
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
end CD_to

Keyboard shortcut for the green "zoom" button

// Ever since I started using OS X as my main OS (back at version 10.1), I kept wishing for a keyboard shortcut for the green button at the top left of each window. I wanted a quick way to maximize a window. Sadly, such a shortcut never came into being. I finally took the time to write an applescript to do the job. You can paste this script into Script Editor and save it in your ~/Library/Scripts/ folder. I saved my script as "Click Green Maximize Button of Frontmost Window.scpt". I then used QuickSilver's Triggers feature to assign the keyboard shortcut Control-Option-Z to run the script. I picked that key combination because it's easy to press. I remember it by thinking of the Z as standing for Zoom. Get QuickSilver here: http://www.versiontracker.com/dyn/moreinfo/macosx/22549

tell application "System Events"
	if UI elements enabled then
		set FrontApplication to (get name of every process whose frontmost is true) as string
		tell process FrontApplication
			click button 2 of window 1
			--button 2 is the green "zoom" button for all applications
			--window 1 is always the frontmost window.
		end tell
	else
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "UI element scripting is not enabled. Check 'Enable access for assistive devices'"
		end tell
	end if
end tell
« Newer Snippets
Older Snippets »
Showing 1-10 of 12 total  RSS