Controlling Applications

You can control multiple applications (including Finder) in one applescript. Each command to control an application must be wrapped in a tell/end tell statement.
Start by typing tell application and then type the name of the application in double quotes. Always use double quotes with variables and tell statements. If applescript isn’t quite sure which application you are referring to, it will bring up a list of apps (after you compile [hammer icon] the script) to choose from. So, don’t stress about calling it photoshop or adobe photoshop or adobe photoshop CC 15.
A best practice to develop is to write end tell right away, that way you have your complete block ready to go. If you don’t follow a tell statement with an end tell statement you will get an error.
tell application “Finder”
end tell
You can put in multiple commands inside the tell application statements, or none at all.
Sample script:

tell application “Finder”
set my_desktop to path to desktop
make new folder at my_desktop with properties {name:”My Folder”}
end tell

tell-end-tell

Put the previous code in the script editor. Compile it using the hammer icon or the enter on your keyboard that is beside your number pad. Press the play (triangle) icon to run the script. It will create a new folder on your desktop named “My Folder”.

Note: If a folder with the same name already exists in that location (in this example a folder named My Folder on the desktop) the script will give you an error when you run it. This would occur if you ran this script and then ran it again. If you want to bypass the error if the folder already exists, but create one if it doesn’t exist, then the easiest way to do this would be to wrap the command in a try – end try statement. 

Back to the Learning Guide

Add your comment

Your email address will not be published. Required fields are marked *