Hey,
Is it possible to open a file in the same directory as your script but without having to locate the whole file path. My application needs to import the contents of a file (which it does fine if you specify the path). But, when I want to send this to a friend I need it so that it will automatically find the file (in the same directory) and open it.
I know this is very easy to do in C, C++ and Obj-C so surely it must be easy in Applescript as well?!?!?!
AppleScript provides a command called ‘path to me’ which returns the file spec of the application running the script. Using this, combined with the Finder, you can easily get to a file in the same folder:
– get the folder containing the script
tell app “Finder” to set myFolder to container of (path to me) as string
– read the contents of a file in the folder
set fileData to read file (myFolder & “filename.ext”)
Mark (Alldritt) is correct but this stumps a lot of new users because when you run this code from within Script Editor, “path to me” returns the path to the Script Editor application and not the script itself. When the script is saved and run as a compiled script (either as an application or through some other method such as the Script menu) then the “path to me” command will return the proper path to the script itself.