Macos applescript函数的问题
我试图在下面的脚本中调用函数replaceChars,但出现以下错误:Macos applescript函数的问题,macos,applescript,finder,Macos,Applescript,Finder,我试图在下面的脚本中调用函数replaceChars,但出现以下错误: error "Finder got an error: Can’t continue replace_chars." number -1708 其目的是将脚本作为按钮添加到finder中,以便我只需单击它即可将路径复制到剪贴板。我在补充file://localhost/ 这样,当通过电子邮件与用户共享时,该链接可以作为指向本地网络上文件夹的直接链接使用。如果可能的话,我还想添加到剪贴板相同的Windows机器 如果您能就上
error "Finder got an error: Can’t continue replace_chars." number -1708
其目的是将脚本作为按钮添加到finder中,以便我只需单击它即可将路径复制到剪贴板。我在补充file://localhost/ 这样,当通过电子邮件与用户共享时,该链接可以作为指向本地网络上文件夹的直接链接使用。如果可能的话,我还想添加到剪贴板相同的Windows机器
如果您能就上述任务提供任何指导,我们将不胜感激,这是我第一次尝试使用applescript编程,因此我不太了解如何完成任务
代码如下:
on appIsRunning(appName)
tell application "System Events"
set isRunning to ((application processes whose (name is equal to appName)) count)
end tell
if isRunning is greater than 0 then
return true
else
return false
end if
end appIsRunning
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
if appIsRunning("Finder") then
tell application "Finder"
set thePath to "file://localhost/" as text
set theTarget to (target of front Finder window) as text
set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text
end tell
end if
AppleScript正在查找程序的脚本字典中查找
replace_chars
处理程序。您可以将其设置为my replace_chars
以在脚本中显示为,或者(可能更好)将设置剪贴板到路径&replace_chars(目标“:”,“/”)作为文本
完全从tell块中删除。AppleScript正在查找程序的脚本字典中查找replace\u chars
处理程序。您可以将其放入my replace_chars
以在脚本中显示为,或者(可能更好)将将剪贴板移动到PATH&replace_chars(目标“:”,“/”)并将其作为文本
行移出tell块。您可以将其放入一行脚本:
tell application "Finder" to set the clipboard to "file://localhost" & (target of front Finder window as text)'s POSIX path
您可以将其制作成一行脚本:
tell application "Finder" to set the clipboard to "file://localhost" & (target of front Finder window as text)'s POSIX path
将您的
Replace_chars(目标“:”,“/”)
调用替换为me的Replace_chars(目标“:”,“/”)
:
set the clipboard to thePath & replace_chars(theTarget, ":", "/") of me as text
将您的
Replace_chars(目标“:”,“/”)
调用替换为me的Replace_chars(目标“:”,“/”)
:
set the clipboard to thePath & replace_chars(theTarget, ":", "/") of me as text
我的答案解决了
替换字符的问题,但不是您所采取的总体方法。一个简短的评论:在路径格式之间转换最好使用。如果脚本是Finder中的一个按钮,那么检查它是否正在运行没有任何意义。您可以很容易地将其制作成两行脚本。您的代码和接受的答案构建的路径不正确。正如Michael在评论中指出的,我在回答中指出的,使用posix路径而不是重建hfs路径。我的答案解决了替换字符的问题,但不是您所采取的总体方法。一个简短的评论:在路径格式之间转换最好使用。如果脚本是Finder中的一个按钮,那么检查它是否正在运行没有任何意义。您可以很容易地将其制作成两行脚本。您的代码和接受的答案构建的路径不正确。正如Michael在评论中指出的,我在回答中指出的,使用posix路径而不是重建hfs路径。我投票赞成thsi 1,因为posix和hfs路径之间的区别不仅仅是路径分隔符。当与其他卷一起使用时,所有其他示例都将失败。我投票支持thsi one,因为posix和hfs路径之间的差异不仅仅是路径分隔符。与其他卷一起使用时,所有其他示例都将失败。