Relative Paths

I am currently using code in this format to open a pdf from a cd via a Flash projector

tell application “Finder”
activate
select file “example.pdf” in folder “pdfs” of disk “CD”
open selection
close windows
end tell

This works fine, but I need to be able to open the pdf even if the user has copied the contents of the CD onto their computer. i.e from any given location.

Is there anyway of returning the path that an applescript has been called from so I know where the files have been copied to. Or any other way of opening a PDF with applescript from a path relative to where the applescript resides.

Thanks

Hi primalthirst,

If the AppleScript is a script application, then you can use the ‘path to’ command:

set my_reference to (path to me)

If you run this in the Script Editor, then it will return a reference to the Script Editor because it returns the reference to the application that is running the statements of the script.

If you want the path to a compiled script, then there is a complicated workaround where you would need a script runner.

gl,

:smiley: Thank you, thank you, thank you Kel. Put me in the right direction and solves a big problem for me.

For anyone interested here is the adpted code that opens a pdf from the same folder as the applescript

tell application “Finder”
activate
set my_reference to container of (path to me)
select file “example.pdf” in my_reference
open selection
close windows
end tell

This is the wrong approach to take to opening a file.

The issues are that a) the Finder has to be activated, b) it manipulates the selection, potentially opening folder windows that the user doesn’t expect and c) is prone to runtime errors if the user performs some action (e.g. changes the selection or changes window focus while your script runs.

Instead, just:

 tell application "Finder"
	set my_reference to container of (path to me) as text
	open file my_reference & "example.pdf"
end tell

This doesn’t activate the Finder. It doesn’t change active windows, open any windows, change the selected item(s). It just does what you want it to.

Thanks alot Camelot I have been unhappy with the whole approach of opening different finder windows. This is much cleaner. Wish I hadn’t just rewritten the 63 seperate scripts I am using before reading your post.