Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Why are Adobe scripting docs like for Illustrator vague on Applescript's file open?

Participant ,
Mar 23, 2015 Mar 23, 2015

From the Adobe scripting reference for Applescript, for Illustrator (I believe is same for other products too), I see code example snippets like:

on openFile(fileToOpen)

    tell application "Adobe Illustrator"

        activate

        open POSIX file fileToOpen as alias with options

    end tell

end openFile

Why does it leave us to figure out the (string) value of fileToOpen? Understandable that that is Applescript specific, but that is more work for those of us not familiar with Applescript to do more research.

Is "as alias" really needed? I'm having trouble with this. Omitting "as alias" gets Applescript error: "Adobe Illustrator got an error: AppleEvent handler failed." Keeping "as alias" results error saying it can't create alias to the path, which for some reason looks like it's formatted as Applescript path even though specified as POSIX (e.g. ":~:Documents:Temp:someFile.ai" instead of "~/Documents/Temp/someFile.ai").

Tried the straightforward example

open POSIX file "~/Documents/Temp/someFile.ai"

as well as

set pfile to "~/Documents/Temp/someFile.ai" as POSIX file

open pfile without options

set pfilepath to "~/Documents/Temp/someFile.ai"

set pfile to POSIX file pfilepath

open pfile as alias without options

nothing seems to work so far. Such a pain.

TOPICS
Scripting
2.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 25, 2015 Mar 25, 2015

That's kind of what I mentioned...it needed a file object

Translate
Adobe
Community Expert ,
Mar 24, 2015 Mar 24, 2015

in javascript, "fileToOpen" expects an actual File Object, not a string.

var file = new File( "C:/Users/carlos/Documents/Illustrator/Forum Questions/missingFontAparatija.ai");

var idoc = app.open(file);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 24, 2015 Mar 24, 2015

Thanks, I'll do more research. Unfortunately, it's not very clear in the Adobe Applescript docs. It doesn't appear to be a file object (and not sure if there is file object in Applescript), so it seems more like string there.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 25, 2015 Mar 25, 2015

Turns out had to do something like this:

set pfilepath to POSIX path of "~/Documents/Temp/test.png"

set pfile to POSIX file pfilepath as text

tell application "Adobe Photoshop CS6"

   activate

   open file pfile without options

end tell

from MacScripter / Pass POSIX file path to Adobe Illustrator to run ExtendScript

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 25, 2015 Mar 25, 2015

Thanks for sharing your findings with the community, sadly there is not much happening anymore around here concerning Applescript questions and answers, MacScripter‌ is the go to resource if you have questions for Applescript.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2015 Mar 25, 2015

That's kind of what I mentioned...it needed a file object

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 26, 2015 Mar 26, 2015

man9ar00 wrote:

Turns out had to do something like this:

  1. set pfilepath to POSIX path of "~/Documents/Temp/test.png" 
  2. set pfile to POSIX file pfilepath as text 
  3. tell application "Adobe Photoshop CS6" 
  4.    activate 
  5.    open file pfile without options 
  6. end tell 

from MacScripter / Pass POSIX file path to Adobe Illustrator to run ExtendScript

That code is very confused and incorrect.

Ignoring Photoshop, as its `open` command is notoriously broken, your original problem is a simple one: AppleScript only understands absolute paths, and does not recognize Unix shell-isms like `~/`. Therefore, you need to write:

    set pfile to POSIX file "/Users/NAME/Documents/Temp/test.png"

    tell application "Adobe Illustrator"

        activate

        open pfile

    end tell

replacing `NAME` with your home folder name. Or, if you don't want to hardcode the home folder name, use:

    set homepath to POSIX path of (path to home folder)

    set pfile to POSIX file (homepath & "Documents/Temp/test.png")

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 30, 2015 Mar 30, 2015

Well, I will say that I tried the long POSIX path too rather than "~" shorthand. That didn't work either with the previous code attempts. The final code posted was what worked.

Thanks for the Photoshop open issue, good to know. I recall that was indeed still broken, so I tested against Illustrator, which is what I'm primarily coding for.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 07, 2015 Apr 07, 2015

man9ar00 wrote:

Well, I will say that I tried the long POSIX path too rather than "~" shorthand. That didn't work either with the previous code attempts. The final code posted was what worked.

Thanks for the Photoshop open issue, good to know. I recall that was indeed still broken, so I tested against Illustrator, which is what I'm primarily coding for.

Using AI CS6 here too. I've *never* had any problem using `POSIX file "/full/path/to/file"`, though I always keep it outside of the `tell application...end tell` block as `POSIX file` specifiers are an AppleScript feature, not a reference to an object in the application's own object model.

(One of AppleScript's myriad warts is the way that AppleScript-specific `date`, `alias`, `POSIX file` specifiers are evaluated outside versus inside of `tell application` blocks. In the first case, you're saying `POSIX f "..." of AppleScript`, which is what you want. In the second, you may be saying `POSIX file "..." of application "..."` without realizing it - the rules by which AppleScript evaluates such specifiers are ridiculously convoluted, with various special-case exceptions and version-specific differences, and all hopelessly undocumented. Huge PITA.)

Occasionally you may run into an older Carbon-era app that absolutely insists on receiving a typeAlias descriptor (supported since System 7), and throws an error if given a typeFileURL ("POSIX file") descriptor instead (introduced with OS X, IIRC), in which case you need to cast it to that type yourself: `POSIX file "/full/path/to/file" as alias`. However, a correctly designed application should not do that: it should tell the Apple Event Manager exactly what type of descriptor it requires (e.g. typeAlias) and let the AEM coerce whatever value the user gives it to that type before unpacking it (or raise an error if the coercion fails). But AI isn't that shoddy AFAIK, so either type should be accepted just fine.

Other than that, make sure you've typed the file path correctly, of course. Sounds obvious, but AI's error reporting isn't always the best, so when an `open` command does fail it gives no clue why. (Drives me up the wall, this one does.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 07, 2015 Apr 07, 2015

Thanks for the insight hhas01, that will be useful to know. I had no idea being inexperienced with Applescript.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 07, 2018 Mar 07, 2018

Hey, im always struggeling with POSIX vs apple, "/" vs ":"

Im more of a tryNerror-codesample-architect, but in the end we have a good script.

Can someone help me out, I need to save a file to our afp-server, but it has a "/" in the filename.
I had me…

tell application "Adobe Illustrator"

    set mydocc to current document

    set newFP to "/Volumes/srvmar101_01/123/myTest.ai"

    save mydocc in newFP

    close mydocc

end tell

…working, but cant get it done with apple references. Just changing…

set newFP to "Volumes:srvmar101_01:123:myTest/99.ai"

doest do the trick. Any quick help?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Mar 08, 2018 Mar 08, 2018

Posix paths translate the slash in filenames as a colon.

Drag your file to a terminal window and you'll see the whole path.

To translate that to the HFS notation, you also omit the "Volumes:" prefix.

For an automated translation, open AppleScript Editor, and run the single line

get (POSIX file "/Volumes/RAMDisk/s:lash") as alias

to see the output

alias "RAMDisk:s/lash"

Of course substitute your path obtained from the terminal above.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 08, 2018 Mar 08, 2018
LATEST

Thank you Dirk, that helped to understand.

I also stumbled upon a as-function, that helped me out quickly to…

​

set newFPafs to "Volumes:myNAS:test/file.ai"
save mydoc in (POSIX path of newFPafs)

…switch from "Volumes:myNAS:test/file.ai" to "/Volumes/myNAS/test:file.ai"

but to illustrate, how less I know what am I doing, the line

set the label index of (((POSIX path of newFPafs) as POSIX file) as alias) to 2

i think is more complicated than it should be, but that worked and speed doesnt really matter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines