Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lynkynpark86

macrumors 6502
Original poster
I am making a program in applescript that opens a file. I know how to just open it:
Code:
set filepath to POSIX path of "Users:student:Desktop:Picture 3.png"
try
	set command to "open " & quoted form of filepath
	do shell script command
end try
but I want to open it with a specific program. IE:
Code:
tell application "system events"
   open "Users:admin:Desktop:orderform.pdf" using application "Some PDF Reader"
end tell
but the above example doesn't work. PLEASE HELP!!!:apple:
 
Try this:

tell application "Finder"
open file ((path to desktop folder as text) & "text.rtf") using ((path to applications folder as text) & "TextEdit.app")
end tell
 
This should work, too:

Code:
tell application "Some PDF Reader"
   open "Users:admin:Desktop:orderform.pdf"
end tell

or even as a one liner:

tell application "Some PDF Reader" to open "Users:admin:Desktop:eek:rderform.pdf"

If it doesn't work, see if the app has an AppleScript dictionary. (Open Dictionary command in Script Editor.) No dictionary, then AS can't control it.

mt
 
  • Like
Reactions: rbrugos
This should work, too:

Code:
tell application "Some PDF Reader"
   open "Users:admin:Desktop:orderform.pdf"
end tell

or even as a one liner:

tell application "Some PDF Reader" to open "Users:admin:Desktop:eek:rderform.pdf"

If it doesn't work, see if the app has an AppleScript dictionary. (Open Dictionary command in Script Editor.) No dictionary, then AS can't control it.

mt
Works perfect and very simple!
 
I am making a program in applescript that opens a file. I know how to just open it:
...
but the above example doesn't work. PLEASE HELP!!!:apple:
One other thing you might try:


AppleScript:
set filepath to POSIX path of "Users:student:Desktop:Picture 3.png"
try
    do shell script "open " & quoted form of filepath & " -a MyImageViewer.app"
end try

That will rely entirely on the `open` command to kick off the app. If you leave "-a AppName.app" off the command, it will open the file in the configured default app for the file type. But if you specify -a and an app name from the Applications folder, MacOS does all the heavy lifting and just opens the file in the app you specified.

Just one other approach to consider. :)

I use this approach in a function I have set up when I am in Bash, looks like this:


Bash:
# Runs at the start of each new Bash session
# creates an alias command so I can just type editbp and my
# .bash_profile comes up in VS Code.

editbp() {
    open ~/.bash_profile -a "Visual Studio Code.app"
}

# Whereas this one expects to receive a file name as an argument which is then
# opened in VS Code. I use this regularly to use my IDE tooling to look at documents 
# that I am working with in VS Code's integrated terminal

edit() {
    open $1 -a "Visual Studio Code.app"
}

# Granted this isn't AppleScript, but it's a valid example of how the open CLI util works
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.