引用文件错误

时间:2017-08-10 18:03:43

标签: applescript

我正在尝试从文件中执行代码,但无法正确路径 引用并获得以下错误。

Safari got an error: 
Can’t get file "Main:Users:Adrian:Documents:Portfolio:automation:explorer:logic.js" of window 1.


我确实尝试过这样做:

tell application "Safari"
    tell window 1
        set current tab to make new tab with properties {URL:"...some"}
        delay 5
        do JavaScript file "Main:Users:Adrian:Documents:logic.js" in current tab
    end tell
end tell


并...

tell application "Safari"
    tell window 1
        set current tab to make new tab with properties {URL:"...some"}
        delay 5
        do JavaScript (file "Main/Users/Adrian/Documents/explorer.js") in current tab
    end tell
end tell

我指定的路径是从文件信息(cmd + i)的应对路径中获取的路径

/Users/Adrian/Documents/logic.js

主要是硬盘名称

1 个答案:

答案 0 :(得分:1)

do JavaScript参数必须为text,因此您可能必须先阅读该文件。顺便说一句:POSIX路径始终以斜杠表示启动卷,而不像冒号分隔的HFS路径以磁盘名称开头。

set jsText to read "/Users/Adrian/Documents/explorer.js" as «class utf8»
tell application "Safari"
    tell window 1
        set current tab to make new tab with properties {URL:"...some"}
        delay 5
        do JavaScript jsText in current tab
    end tell
end tell