AppleScriptlets: Homemade Actions

You have Duplos and you have Legos, just like you have Automator and you have AppleScript. Automator includes the Run Applescript action, so you can extend the power of Automator. These scriptlets will allow you create extra Actions without having to actually learn scripting. Some of these scripts may appear to duplicate 3rd-party actions you'll find at Apple or Automator World. They're included here because workflows and Automator applications won't work on other machines unless that action is installed. An Applescript that does the same function gets around that, since it stays with the workflow.

You'll see "(Requires UI scripting enabled.)" with many of these scriptlets. Click here for a brief explanation and an Automator app that does most of the enabling process for you.


Address Book: Back Up
This will back up your Address Book to your desktop. By default it chooses the desktop. If you want to change this, do a manual back up and choose a different location. Address Book should remember the next time. Double click on the back up to restore. Useful to put this action in front of any pseudoaction you may create that tampers with the Address Book database. (Requires UI scripting enabled.)
--Back Up Address Book
tell application "System Events"
tell process "Address Book"
tell menu bar 1
click menu item "Back up Address Book…" of menu "File"
delay 3
keystroke return

end tell
end tell
end tell

Address Book: New Card
This action doesn't populate the entry, yet. But it does give you a start on an action that's missing from Automator. (Requires UI scripting enabled.)
--New Address Book Card
tell application "Address Book" to activate
tell application "System Events"
tell process "Address Book"
keystroke "n" using command down
end tell
end tell

Any App: Quit
This quits the stated app. Useful for cleanup after Automator has left a mess of open apps it's used. This scriptlet is for iPhoto. You can change the word iPhoto to iTunes, or whatever app you want to quit, provided the app has the Apple-H shortcut. Make sure your app name is in quotes.
--Quit App
tell app "iPhoto" to quit
end tell

Any App: Quit Without Saving Sometimes Automator will leave an empty message or loose new mail item on the desktop. This scriplet will quit the app without saving. Replace "Mail" with TextEdit or whatever app necessary.
--Quit App Without Saving
tell application "Mail"
quit without saving
end tell

Any App: Hide This minimizes the stated app. Useful for when Automator puts and app that it's using onscreen and you'd rather not see it. This scriptlet is for iPhoto. You can change the word iTunes to iPhoto, or whatever app you want to hide, provided it has the Apple-H shortcut. Make sure your app name is in quotes. (Requires UI scripting enabled.)
--Hide App
tell app "System Events"
tell app "iTunes" to activate
tell process "iTunes" to keystroke "h" using command down
end tell
end tell

Any App: Show You may need to show a hidden app to work with it. This will bring it out of its hiding place. Replace TextEdit with the name of the app you want to show. This will also start up a closed app. (Requires UI scripting enabled.)
--Show App
tell app "System Events"
tell app "TextEdit" to activate
end tell


Automator: Pop Box (thx: Tom X) In the spirit of automation, this temporarily pops up a message box. If a process takes a long time, this will let the user know, but not require a click to clear the box. For now, this will have to do in place of a progress indicator. Put what you want in TextGoesHere and change 2 to the number of seconds you want the box to be on the screen. You'll want to leave it up long enough for a comfortable read, but not so long that they get bored and have to click the OK button.
--Pop Box
display dialog "TextGoesHere" giving up after 2

Clipboard: Get contents of This scriptlet retrieves the contents of the clipboard. Not as useless at it seems. You can use this in conjunction with the Copy to Clipboard action (System) to store file references for calling later. Very useful if your workflow does something that takes Finder item names out of the Data Flow.
--Return Clipboard Contents to Data Flow
return (the clipboard)

Clipboard: Show This shows what's on your clipboard.
--Show the Clipboard
display dialog (the clipboard)

Finder: Dupe file Duplicates a folder or file to the same location and add "copy" to the end of the filename. Copy Finder Items can't copy to the same location.
--Dupe a File or Folder
tell app "Finder"
duplicate selection
end tell

Finder: Empty trash This will empty the Mac's main Trash. Can be useful to keep finder from finding a second copy of a document in the Trash.
--Empty Trash
tell app "Finder"
empty the trash
beep
end tell

Finder: Make alias This makes an alias of the file on the desktop. (thx: Ric Latham)
--Make Alias
tell application "Finder"
set theFile to input
set theAlias to make new alias file at desktop to theFile
end tell


iCal: Backup Backs up iCal to Desktop. (Requires UI scripting enabled.) (thx: Kim Hunter.)
--Backup iCal
tell app "iCal" to activate
delay 2
tell app "System Events"
tell menu item "Back up Database…" of menu "File" of menu bar 1 of application process "iCal" to click
delay 3
keystroke "iCal"
keystroke "d" using command down
delay 2
keystroke return
end tell

Imagine Photo: Set Image DPI Mac sets everything at 72 dpi. If you need a higher res, as in PDF's that zoom, this will do it. Requires that you have Imagine Photo freeware installed. Replace the entire script in the Run AppleScript action. (thx: Kevin Meaney of Yarra Valley Software)
on run {input, parameters}
--Set DPI in Imagine Photo.

-- This will replace the contents of the original file.
tell application "iMagine Photo"
repeat with theItem in input

set thisImporter to import graphic theItem
-- error checking - only process files that quicktime knows about.
if the component error of thisImporter is equal to 0 then
my CreateReplacingExporterLikeImporter(thisImporter)
set the export resolution of thisImporter to {150.0, 150.0}
-- set the export custom icon of thisImporter to true

export thisImporter
end if
close thisImporter -- the importer needs to be closed whether an error occured or not
end repeat
end tell
return input
end run
on GetExportTypeFromImportType(theimportType)
if theimportType is equal to "Photo - JPEG" then

return "JPEG"
else if theimportType contains "TIFF" then

return "TIFF"
else if theimportType is equal to "QuickDraw" then

return "PICT"
else if theimportType is equal to "Planar RGB" then

return "Photoshop"
else if theimportType is equal to "JPEG 2000" then

return "JP2"
else

return theimportType
end if
end GetExportTypeFromImportType
on CreateReplacingExporterLikeImporter(theInitialImporter)
tell application "iMagine Photo"
try

tell theInitialImporter
set theImporterType to the graphic type
set exportType to my GetExportTypeFromImportType(theImporterType)
make exporter with properties {export file type:exportType}
if the export component error is not equal to 0 then

set the export file type to "TIFF"
end if

set exportProperties to {}
if (exportType is equal to "JPEG") then

set exportProperties to exportProperties & {export compression quality:lossless, export exif user data:file location as alias}
else if theImporterType is equal to "TIFF (Uncompressed)" then

set exportProperties to ¬
{export compression method:not packbits, export exif user data:file location as alias}
end if

if the image count is equal to 2 then
set exportProperties to exportProperties & {export thumbnail state:{1, 128, 128}}
end if

set exportProperties to exportProperties & {export file location:file location as alias}
set export properties to exportProperties

end tell
end try
end tell
end CreateReplacingExporterLikeImporter
iPhoto: Switch library (thx: Justin S.) iPhoto lets you switch libraries when you start it by holding down the option and the shift key. This AppleScriptlet duplicates that key combination. (Requires UI scripting enabled.)
--Switch iPhoto Library
tell application "System Events"
key down option

key down shift
tell application "iPhoto"
activate

tell application "System Events"
key up option

key up shift
delay 1
keystroke return

end tell
end tell
end tell

iPhoto: Empty trash This empties iPhoto's trash without warning. If you'd like to add the warning, just delete the delay 1 and keystroke return lines from the script. (Requires UI scripting enabled.)
--Empty iPhoto's trash
tell application "System Events"
tell application "iPhoto" to activate
tell process "iPhoto" to keystroke (ASCII character 8) using shift down & command down
delay 1
keystroke return
end tell

iPhoto: New slideshow Creates a new slideshow in iPhoto with a chosen name. Replace NameGoesHere with your choice. (Requires UI scripting enabled.)
--Create New iPhoto Slideshow
tell application "iPhoto"
activate

end tell
tell application "System Events"
tell process "iPhoto"
tell menu bar 1
tell menu bar item "File"
tell menu 1
click menu item "New Slideshow"
keystroke "NameGoesHere"
keystroke return

end tell
end tell
end tell
end tell
end tell

Preview: Get PDF contents Copies the contents of a PDF open in Preview and puts them on the clipboard. Copies images as well. closes PDF Text and images could then be pasted into TextEdit. (Requires UI scripting enabled.)
--Copy PDF Contents
tell application "System Events"
tell process "Preview"
keystroke "a" using command down

delay 1
keystroke "c" using command down
delay 1
keystroke "q" using command down

end tell
end tell

Safari: Email current Web address to self This puts the URL of the current page in Safari in the body of an email and sends it to the address specified in the script. Useful for quickly forwarding links from one computer to another. If you have the BCC field visible, you'll need to insert another "keystroke tab" between the two already there. (Requires UI scripting enabled.)
--Email Current Web Address to Self
tell application "System Events"
tell application "Safari" to activate
tell process "Safari" to keystroke "i" using command down & shift down
delay 1
keystroke "YourEmail@YourDomain.com"
keystroke tab
keystroke tab
keystroke "Web Address"
keystroke "d" using command down & shift down
delay 1
keystroke "h" using command down
end tell

Safari: Email Web quote to someone Highlight some text on a Web page and this scriptlet will open a mail message, insert the selected text in quotes, put the URL in the message below the quote and set the subject line as "Web Quote". You fill in the address like normal and send. If you want to set up the workflow hardcoded to send the quote to yourself or someone else, change "YourEmail@YourDomain.com" (keep the quotes) to the address you'd like to send to and delete all the double dashes in the script, except the first set. (Requires UI scripting enabled.)
--Email Web quote to someone
tell application "System Events"
tell application "Safari" to activate

tell process "Safari" to keystroke "c" using command down
keystroke "i" using command down & shift down
delay 1
--keystroke "YourEmail@YourDomain.com"
keystroke tab

keystroke tab
keystroke "Web Quote"
keystroke tab

keystroke "v" using shift down & command down
keystroke return
--keystroke "d" using command down & shift down
--delay 1
--keystroke "h" using command down

end tell

Safari: Empty cache This dirty little trick will empty Safari's cache, whether Safari is running or not. (Requires UI scripting enabled.)
--Empty Safari's cache
tell application "System Events"
tell application "Safari" to activate
tell process "Safari" to keystroke "e" using command down & option down
delay 1
keystroke return
end tell

Safari: Open downloads manager This calls up Safari's Downloads Manager pane. Keeps Safari from getting stuck when downloading files from links. (Requires UI scripting enabled.)
--Open Safari Downloads Manager
tell application "Safari" to activate

tell application "System Events"
tell process "Safari"
keystroke "l" using option down & command down

end tell
end tell

Safari: Get source code of current page Gets the source rather then the text.
--Get Page Source From Safari
on run {input, parameters}
tell application "Safari" to set my_html to source of document 1
return my_html

return input
end run

Sticky: Paste into new This pastes what's on the clipboard into a new Sticky note. Running it again while the note is open will paste at the end of the note. Works with styled text and images. (Requires UI scripting enabled.)
--Paste into a new Sticky
tell application "System Events"
tell application "Stickies" to activate
tell process "Stickies" to keystroke "v" using command down
end tell

Safari: Save current page Save screen pops up and allows you to choose name, location and format to save. --Save Current Safari Page
tell application "System Events"
tell process "Safari"
keystroke "s" using command down

end tell
end tell

TextEdit: ASCii characters (thx: Trashman) Not sure what use this might be, but it returns all the ASCii characters. Might me useful for in showing fonts.
set the_string to ""
repeat with i from 32 to 255
set the_string to the_string & (ASCII character i)
end repeat
return the_string

TextEdit: Find and replace
Finds and replaces all instances. Put the text to find and replace between the quotes indicated. (Requires UI scripting enabled.)
--Find and Replace in TextEdit
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
tell window "Find"
keystroke "PutTheTextYouWantToFindHere"
keystroke tab
keystroke "PutTheTextYouWantToReplaceItWithHere"
click button "Replace All"
end tell
end tell
end tell


TextEdit: Insert date and time Puts the date and time in the current Textedit cursor position. (Requires UI scripting enabled.)
--Insert date and time into TextEdit
set date_ to (current date) as string
tell application "System Events"
tell application "TextEdit" to activate
tell process "TextEdit" to keystroke date_
end tell

TextEdit: Move cursor to end of document By default, TextEdit documents open with the cursor at the beginning. This moves it to the end of the current document. (Requires UI scripting enabled.)
--Move cursor to end of TextEdit document
tell application "System Events"
tell application "TextEdit" to activate

tell process "TextEdit"
keystroke (ASCII character 31) using command down

end tell
end tell

TextEdit: Paste into (courtesy of Tom X). This will paste an image or text on the clipboard into the cursor position of an open document. Can be used to build a document out of images, text and other documents. (Requires UI scripting enabled.) Note the two lines where you must remove hyphens if running as a workflow.
--Paste into TextEdit
--tell application "TextEdit" to activate--remove hypens before "tell" if running as a workflow
tell application "System Events"
tell process "TextEdit" to keystroke "v" using command down
end tell
--end tell --remove hypens before "end" if running as a workflow

TextEdit: Save the current document as HTML If you've opened a named doc it will save it by its name. If it's a new doc, it will be named "Unitled?" with numbers where the ? is. Any formatting in an RTF or Word files is retained in the HTML. Note the two lines where you must remove hyphens if running as a workflow. (Requires UI scripting enabled.)
--Save TextEdit Doc as HTML
--tell application "TextEdit" to activate--remove hypens before "tell" if running as a workflow

tell application "System Events"
tell process "TextEdit"
keystroke "S" using command down

delay 1
click pop up button 1 of group 1 of sheet 1 of window 1
delay 1
key code 125
delay 1
keystroke return

delay 1
keystroke return

end tell
end tell
--end tell --remove hypens before "end" if running as a workflow

TextEdit: Save the current document Automator tends to work with open TextEdit docs, but there's no action to save the doc. Good to use with the New Text File action, since it creates a file with an identifiable name.
--Save TextEdit Doc
tell app "TextEdit" to save the front document quit


TextEdit: Show TextEdit If you hide TextEdit, some of Automator's TextEdit look for the active TextEdit document but can't find it. If you've hidden it, you'll need to show it to work with it. (Requires UI scripting enabled.)
--Show TextEdit
tell application "System Events"
tell application "TextEdit" to activate

end tell

UI Scripting: Turn on helper UI scripting turns loose many AppleScript features. Unfortunately it's turned off until you turn it on. This script will bring up the Preferences Universal Access window and a dialog telling the user to make sure it's checked. This scriptlet is very handy when you distribute an Automator app that uses UI scripting. Very nice little scriptlet, which I cannot take credit for. Modified from a script found on the Bayport NY Fire Department site:
--UI Scripting helper
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "Make sure \"Enable access for assistive devices\" below is checked to run this app."
end tell

UI Scripting: Turn on helper (Improved) This checks to see it UI is enabled. If so, it tells you the app is ready to run. If not, it launches the screen with the UI checkbox and instructs the user to turn it on. If you're sharing your app. It's best put this in a Run Applescript action in a workflow by itself and save it with a title like "Click here to set up app."
--Alt UI scripting helper
tell application "System Events"
set UI_enabled to UI elements enabled

end tell
if UI_enabled is false then
tell application "System Preferences"
activate

set current pane to pane "com.apple.preference.universalaccess"
display dialog "Please check the \"Enable access for assistive devices\" box below."
end tell

else
display dialog "App is ready to run."
end if

return input
end run