首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译时,Applescript的行为有所不同。

编译时,Applescript的行为有所不同。
EN

Stack Overflow用户
提问于 2022-10-30 21:31:53
回答 1查看 36关注 0票数 0

这部分Applescript:

代码语言:javascript
复制
on collectionToJson(_collection)
  tell application "Capture One 22" to return "{\"name\": " & "\"" & name of _collection & "\", \"id\": " & id of _collection & ", \"kind\": " & "\"" & kind of _collection & "\", \"user\": " & user of _collection & "}"
end collectionToJson

运行、解释和编译时的行为有所不同。以解释模式输出:

代码语言:javascript
复制
> osascript target/classes/Collections.applescript --byId 14659
{"name": "StoppingDown", "id": 14659, "kind": "project", "user": true}

以编译模式输出:

代码语言:javascript
复制
> osascript target/classes/Collections.scpt --byId 14659
{"name": "StoppingDown", "id": 14659, "kind": "«constant ****CCpj»", "user": true}

类属性(即枚举)以不同的方式输出。这取决于我对Applescript缺乏的知识还是与我交谈的特定应用程序?

完整脚本:

代码语言:javascript
复制
on run (_args)
    tell front document of application "Capture One 22"
        set _command to item 1 of _args
        
        if _command is "--byId" then
            set _collection to my findCollectionByIdPath(_args)
            my collectionToJson(_collection)
            
        else if _command is "--byName" then
            set _collection to collection named (item 2 of _args)
            
            repeat with _i from 3 to count of _args
                tell _collection to set _collection to collection named (item _i of _args)
            end repeat
            
            my collectionToJson(_collection)
            
        else if _command is "--children" then
            set _collection to my findCollectionByIdPath(_args)
            tell _collection to my collectionsToJson(collections)
            
        else if _command is "--variants" then
            set _collection to my findCollectionByIdPath(_args)
            tell _collection to my variantsToJson(variants)
        else
            log "Unknown command: " & _command
        end if
    end tell
end run

on findCollectionByIdPath(_args)
    tell front document of application "Capture One 22"
        set _collection to first collection whose its id is item 2 of _args
        
        repeat with _i from 3 to count of _args
            tell _collection to set _collection to (first collection whose its id is (item _i of _args))
        end repeat
        
        return _collection
    end tell
end findCollectionByIdPath

on collectionsToJson(_collections)
    set _result to "["
    set _separator to ""
    repeat with _collection in _collections
        set _result to _result & _separator & my collectionToJson(_collection)
        set _separator to ","
    end repeat
    return _result & "]"
end collectionsToJson

on collectionToJson(_collection)
    tell application "Capture One 22" to return "{\"name\": " & "\"" & name of _collection & "\", \"id\": \"" & id of _collection & "\", \"kind\": " & "\"" & kind of _collection & "\", \"user\": " & user of _collection & "}"
end collectionToJson

on variantsToJson(_variants)
    set _result to "["
    set _separator to ""
    repeat with _variant in _variants
        set _result to _result & _separator & my variantToJson(_variant)
        set _separator to ","
    end repeat
    return _result & "]"
end variantsToJson

on variantToJson(_variant)
    -- FIXME: it seems that this 'tell' slows down a lot
    tell application "Capture One 22" to return ("{\"name\": \"" & (name of _variant) as text) & "\", \"id\": \"" & id of _variant & "\", \"rating\": " & rating of _variant & ", \"colorTag\": " & color tag of _variant & "}"
end variantToJson
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-31 08:18:53

这种行为是正常的,也是意料之中的。AppleScript只在编译和反编译脚本时加载应用程序字典。它在执行编译好的脚本时不会加载字典,因为它不需要它。

编译后的脚本将应用程序定义的关键字存储为“四字符代码”,例如关键字project映射到代码CCpj,当字典定义不可用时,AppleScript将其显示为«constant ****CCpj»

尽管可以“强制”加载应用程序的字典,即使不需要它(使用run script命令编译针对该应用程序的简单脚本),但显而易见的简单解决方案是自己将关键字转换为字符串:

代码语言:javascript
复制
to formatKind(aKind)
  if aKind is project then
    return "project"
  else if ...
    ...
  else
    return aKind as string
  end
end formatKind

或者,在您的特殊情况下,由于您是通过osascript运行脚本的,所以请继续使用未编译的形式:与早期的系统7天不同,编译需要相当长的时间,现代机器的速度足够快,对运行时间没有明显的影响。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74256917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档