POSIX file Returning scpt Object

I’m baffled about some code that works fine in Script Debugger but fails when run within an ASOC app.

When the following is run in Xcode, the “POSIX file” assignment returns a ‘scpt’ object instead of ‘furl’.


set x to POSIX file "/"
log class of x
-- <NSAppleEventDescriptor: 'scpt'>
log x
-- <AppDelegate @0x608000225f20: OSAID(4) ComponentInstance(0x810000)>

log class of (POSIX file "/")
-- <NSAppleEventDescriptor: 'furl'>
log (POSIX file "/") as string
-- Can't make «script» into type string. (error -1700)

In Script Debugger I get what I would expect:


set x to POSIX file "/"
log class of x
-- <<class furl>>
log x
-- "Macintosh:HD"

log class of (POSIX file "/")
-- <<class furl>>
log (POSIX file "/") as string
-- "Macintosh:HD"

What’s going on here?

Thanks,
Lance

In AppleScriptObjC you have to write

 set x to  "/" as POSIX file

POSIX file x does not work

Ah, researching that further, it’s the difference between “coercion” versus an “object specifier”. A subtle but important concept I have yet to learn!

Frustrating the developer docs don’t mention the “as POSIX file” notation.

https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReferenceFilesandFolders.html

Thanks!

PS. Any insight as to why the execution environment of Xcode behaves so different from Script Debugger in this case?

Simply spoken

“ The ScriptDebugger environment is AppleScript.
“ The Xcode environment is Objective-C bridged to AppleScript.

Further to Stefan’s answer:

AppleScript’s log command does nothing itself. What you see logged in Script Debugger or Script Editor is produced by the editor itself, which retrieves a “human readable” form of the AppleScript object in question.

In Xcode, log takes advantage of the normal NSLog-style logging system, which requires Cocoa objects, so AppleScript objects first have to be converted to Cocoa equivalents.

Thanks for the clarification. I just equated ‘log’ to doing a ‘printf’ but was always curious why Script Debugger output is not formatted as just a series of log lines. Now I know it’s because SD is free to do it’s own thing with log statements!

The Xcode environment is running your newly build application against Xcode’s debugger. A debugger like in SD is not debugging a process but debugging AppleScript’s own VM. This is the big difference between these two.

SD is like debugging JavaScript code using Chrome’s built-in development tools.
Xcode is like running Chrome in GDB trying to debug JavaScript code using GDB.