Quick Applescripts

I’ve toyed with AppleScript for years now, usually just to do weird things that aren’t easily accessible like automating iTunes Music Store contest entries, setting the desktop automatically. You know, generally useless things that at the time, present themselves as minor challenges that I must solve.

Here are a few quick AppleScripts for you that I use on a regular basis.

Get the file path of the selected item in the Finder. (does not work with multiple selections)


on run
tell application "Finder"
set selectedItem to (item 1 of (get selection))
set infoList to {}
copy ((selectedItem as alias) as string) to end of infoList

-- this section is commented out of the code so that you only get the path to the selected item. if you want more than that, just remove the parenthesis and asterisk before and after the code

(*

copy ("Displayed Name: " & displayed name of selectedItem) to end of infoList
copy ("Kind: " & kind of selectedItem) to end of infoList
copy ("Size: " & size of selectedItem & " (" & physical size of selectedItem & ")") to end of infoList
copy ("Created: " & creation date of selectedItem) to end of infoList
copy ("Modified: " & modification date of selectedItem) to end of infoList
copy ("Name & Extension: " & name of selectedItem) to end of infoList
copy ("Locked: " & locked of selectedItem) to end of infoList
copy ("Comments: " & comment of selectedItem) to end of infoList
copy ("Owner: " & owner of selectedItem) to end of infoList
copy ("Group: " & group of selectedItem) to end of infoList

*)

-- end commented code

end tell
set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
set infoAsString to infoList as string
set AppleScript's text item delimiters to od
set the clipboard to infoAsString
return infoAsString
end run

Here’s one that I didn’t write but is alot of fun with Exposé. If you want to call one of the Exposé functions from an AppleScript, you can do the following (Note: This script assumes you have the default Exposé key bindings; if you’ve changed them, this will not work as expected!):


-- All windows
tell application "System Events" to key code 101
-- Application windows
tell application "System Events" to key code 109
-- See desktop
tell application "System Events to key code 103
-- See desktop in slow motion
tell application "System Events" to key code 103 using shift down

For example, you can freak out people with this 🙂


tell application "System Events"
set ap to 109
set desk to 103
set win to 101
repeat with a in ¬
{ap, desk, win, win, desk, ap, desk, ap, ap, ¬
ap, desk, win, ap, desk, desk, win, ap, desk, desk}
key code a
end repeat
end tell

Just copy and paste into the AppleScript Script Editor and have fun.

Oh, and here’s the script I wrote to automate my entries for the iTunes Music Store giveaway:


property target_URL :
"https://phobos.apple.com/WebObjects/MZFinance.woa/wa/
com.apple.jingle.app.finance.DirectAction/tellAFriend?playlistId=2859605"

repeat 10 times

tell application "Safari"
activate
open location target_URL
end tell

delay 5

tell application "System Events"
tell process "iTunes"
keystroke "iTunes100@apple.com"
keystroke " "
keystroke "Matt Binkowski"
keystroke " "
keystroke "iTunes@www.mattbinkowski.com"
keystroke return
end tell
end tell

delay 4

end repeat