Basics of AppleScript

Opening Files

Figure 12.3.1

Figure 12.3.1 Path to File

Script [12.3.1]:

tell application "Finder"
    open "Macintosh HD:Users:nayan:Desktop:text.rtf"
end tell

Explanation: The above set of commands ask Finder to open file which is located in desktop.

Figure 12.3.1-2

Figure 12.3.1-2 Opening a File

Script [12.3.2]:

tell application "Finder"
    set thePath to file "Macintosh HD:Users:nayan:Desktop:text.rtf"
end tell

Explanation: Here Finder, sets the path of the file text.rtf to a variable named thePath. The output of path is awkward and can be understood by Finder only.

Figure 12.3.2

Figure 12.3.2 Path to File (Variable)

Script [12.3.3]:

tell application "Finder"
    set thePath to a reference to file "Macintosh HD:Users:nayan:Desktop:text.rtf"
end tell

Explanation: thePath will store reference to the file text.rtf which is located on Desktop. The output says file and not alias.

Figure 12.3.3

Figure 12.3.3 Reference to a File

if the Script was,

choose file

then output would be,

Choose File

Choose File Reference

Figure: Choose a File

Explanation: Important thing to note here is the alias. So what’s the big deal. Well consider a file in Downloads with a name text.rtf. Now you create an alias of text.rtf on the Desktop.

When you open the alias, the file text.rtf opens. Let’s move text.rtf to Documents. Now if you open the alias on Desktop, text.rtf will still open. How is that possible?

Every file on your Mac has a unique id which Finder maintains. The alias does not store the path. It stores the ID. The Finder updates its database with the new path and points it to the File ID.

So when we double click the alias, we are asking Finder to open file with this id.

Script [12.3.4]:

tell application "Finder"
    move file "Macintosh HD:Users:nayan:Desktop:text.rtf" to "Macintosh HD:Users:nayan:Downloads:"
end tell

Explanation: Here Finder, will move text.rtf from Desktop to Downloads folder.

Figure 12.3.4

Figure 12.3.4 Original File Location

Figure 12.3.4-2

Figure 12.3.4-2 Move Files

Script [12.3.5]:

tell application "Finder"
    move file "Macintosh HD:Users:nayan:Desktop:text.rtf" to trash
end tell

Explanation: Here Finder, will move text.rtf from Desktop to trash.

Script [12.3.6]:

set thePath to alias "Macintosh HD:Users:nayan:Desktop:text.rtf"
tell application "Finder"
    move file "Macintosh HD:Users:nayan:Desktop:text.rtf" to "Macintosh HD:Users:nayan:Downloads:"
    open thePath
end tell

Explanation: In this case I have created an alias of text.rtf and stored it in thePath. Then I asked Finder to move text.rtf from Desktop to Downloads.

Finally I open the file thePath. But if you notice I just moved it. However the file will still open as thePath contains the ID not the path.

An important thing to note is that the alias command can be used without the tell application “Finder” block. Though Finder is responsible for indexing files on Mac, the command can be used without the tell block.