Cannot open a file with AppleScript

I'm following the same instruction as listed on this page and yet I cannot open this text file


tell application 

  "TextEdit" open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r.rtxt" 

end tell


The error message I'm getting is: Expected “given”, “with”, “without”, other parameter name, etc. but found class name.


Ultimately I need to be open it with a python script. So when I use Python I get a different error message:


file='Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt'


os.system(

... f'''/usr/bin/osascript -e

... 'tell application

... "TextEdit" open file "{file}"

... end tell' ''')

/usr/bin/osascript: option requires an argument -- e

usage: osascript [-l language] [-e script] [-i] [-s {ehso}] [programfile] [argument ...]

sh: line 3: tell application

"TextEdit" open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt"

end tell: command not found


I should also add that I'm certain the file exists because when I put in a bogus file I get the following error message: No such file or directory

MacBook Pro 15″, macOS 10.12

Posted on Jun 15, 2020 5:49 PM

Reply
Question marked as Top-ranking reply

Posted on Jun 15, 2020 6:17 PM

I got it:

State with the Applescript is in the variable script such as:

 from subprocess import Popen, PIPE
file = 'hey_you.rtxt"
    script =  f'''
    tell application "TextEdit"
        open file "{file}"
    end tell
    '''

Then:

p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
...stdout, stderr = p.communicate(script)


Similar questions

6 replies
Question marked as Top-ranking reply

Jun 15, 2020 6:17 PM in response to kyle76

I got it:

State with the Applescript is in the variable script such as:

 from subprocess import Popen, PIPE
file = 'hey_you.rtxt"
    script =  f'''
    tell application "TextEdit"
        open file "{file}"
    end tell
    '''

Then:

p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
...stdout, stderr = p.communicate(script)


Jun 15, 2020 6:03 PM in response to kyle76

Ok, I can open it with Script Debugger but that's useless because I need to be able to execute the apple script with Python. When I run


tell application "TextEdit"

open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt"

end tell


On Script Debugger it works. But the Python syntax:


os.system(

... f'''/usr/bin/osascript -e

... 'tell application "TextEdit"

... open file "{file}"

... end tell' ''')

/usr/bin/osascript: option requires an argument -- e

usage: osascript [-l language] [-e script] [-i] [-s {ehso}] [programfile] [argument ...]

sh: line 3: tell application "TextEdit"

open file "Macintosh HD:Users:kylefoley:codes:byu_docs:dictionaries:first_name_research:btn_tree_start_r:Jaxson.rtxt"

end tell: command not found


Also, the following fails:

os.system(

... f'''/usr/bin/osascript -e 'tell application "TextEdit" open file "{file}" end tell' ''')

28:32: syntax error: Expected end of line but found command name. (-2741)

os.system(

... f'''/usr/bin/osascript -e 'tell application "TextEdit" open file "{file}" ' ''')

28:32: syntax error: Expected end of line but found command name. (-2741)


I know Python can use Applescript because the following works:


os.system(
    f'''/usr/bin/osascript -e 'tell app "TextEdit" to save (every window whose name is "{file}")' ''')




This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Cannot open a file with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.