“POSIX file” works in Applescript Editor, not in XCode

旧时模样 提交于 2019-12-11 06:46:48

问题


Try this:

  1. Make a new XCode4 Applescript project. In the delegate, paste this code:

    on doIt_(sender)
        set goodHFSLoc to (path to desktop folder)
        set test1 to (POSIX path of goodHFSLoc)
        log "test1:"&test1
        set theJSPath to "/Users/dave/Desktop/MakeTSLabels.js"
        set jsHFSFile to (POSIX file theJSPath)
        set test2 to (POSIX path of jsHFSFile)
    end doIt_
    
  2. Hook this method up to a button in the UI window.

  3. Run the program
  4. Click the button, and you should get this error:

    Can’t get POSIX path of class "NSObject".
    
  5. Put the same code (minus the "on" and "end" lines) into AppleScript editor, and it runs fine.

Apparently, "POSIX file" in ApplescriptObjC doesn't make a file object as the language specification requires. Instead it makes an NSObject.

I need to have an applescript file specifier to provide to Adobe Illustrator's do javascript command, and I need to use NSBundle's functions to get the javascript file, which is packaged in my application bundle.

Am I doing something wrong?


回答1:


You will see the same behavior in a Finder or System Events tell statement. The solution is the same for ASObjC - you need to use it as a coercion:

set jsHFSFile to (theJSPath as POSIX file)


来源:https://stackoverflow.com/questions/9238480/posix-file-works-in-applescript-editor-not-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!