How to open PDF file with Applescript

Posted on 2008-10-24
Last Modified: 2010-04-29
Hi, I am trying to open a PDF on a CD with an Applescript saved in the same directory as the script.

The attached code doesn't work, and I suspect I'm not giving enough of a path to the 'open' function.

Any pointers? I want to be able to do this without buying/installing any custom scripts/components/apps.

tell application "Finder"
open document file "test.pdf"
end tell

Open in new window

Question by:SatelliteCreative
  • 3
  • 2

Expert Comment

ID: 22795845
Okay assuming I am following you correctly.  You want a applescript that will open a pdf located in the same folder as the script on a CD right?

If so give this a try.

set myPath to path to me as Unicode text
set {tid, text item delimiters} to {text item delimiters, ":"}
set vol to text items 1 thru -2 of myPath as string
set text item delimiters to tid
tell application "Finder" to open file (vol & ":test.pdf")

Open in new window


Author Comment

ID: 22796041
Ok thanks, can you tell me whether I should be able to test that from a folder NOT on the CD? I've tried to run it and I get the error:

Finder got an error: Cant get file \"Macintosh HD:Users:Monolith:Desktop:Script\"."

so I imagine its looking for the pdf IN the script editor?

Accepted Solution

dscl earned 500 total points
ID: 22796111
Ahh, script editor!  Script editor has a long standing bug that causes path to me to refer to Script Editor rather than the script itself.  When you deploy your script are you going to be doing so as a AppleScript Application (self-running) or thru a script that actually needs to be ran thru the editor.

If you are doing an application (File > Save As > Application) then everything will work.  If you intend to run the script thru the editor then we need to work up another solution.  If you always know the CD name this would be trivial to do.

About the bug though.  If you do a lot of AppleScripting I would recommend you take a look at a product called Script Debugger.  It's not free, but it's a godsend for AppleScripters and doesn't contain many of the bugs (including this one) that Script Debugger does.

Author Comment

ID: 22796857
Hi, it's fine to work standalone as an application, so now I've saved as you suggest it works perfectly.

Unfortunately now I can't get flash's fscommand to run the script from a 'projector' file! Anyway, I'll probably post another question about this.

Thanks very much for your help!


Expert Comment

ID: 22796916
you might try running it as a script thru the fscommand.  I'm honestly not sure what would happen in that case, but it might, and hopefully would, work as we want/expect it too.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question