【问题标题】:How to use a variable in a POSIX Path with AppleScript?如何在带有 AppleScript 的 POSIX 路径中使用变量?
【发布时间】:2015-10-26 03:33:15
【问题描述】:

我一直在尝试用变量替换 POSIX 路径中的一个部分,但我没有成功。这是我正在尝试开发的脚本:

tell application "Finder"
    duplicate POSIX file "/Users/**Variable**/Documents/img.jpg" to POSIX file "/Users/**Variable**/Desktop/" with replacing
end tell

路径中的部分是我希望变量存在的地方。我该怎么做?

【问题讨论】:

  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅 [How to Ask] (stackoverflow.com/help/how-to-ask) 页面以获得澄清此问题的帮助。
  • 所以我想要的不是粗体字实际上是一个目录,而是我希望它们成为一个变量
  • 使用相对路径的简单语法:duplicate file ((path to documents folder as text) & "img.jpg") to desktop with replacing

标签: path applescript finder


【解决方案1】:

你用 & 和变量分隔字符串。

此外,最好将 posix 路径转换为 ​​Mac 标准冒号:在脚本 Finder 之前的路径。 Finder 的重复功能从 10.9 开始就存在这些问题。 我已经打破了一些强制,所以你可以看到发生了什么。

-- define file and user names
set fileName to "img.jpg"
set user1 to "Mitc"
set user2 to "Mitc"

-- create posix path strings
set path1 to "/Users/" & user1 & "/Documents/" & fileName
set path2 to "/Users/" & user2 & "/Desktop/"

-- convert posix paths into colon paths
set ref1 to (POSIX file path1) as string
set ref2 to (POSIX file path2) as string

-- display dialog ref1 & return & ref2 -- to test
tell application "Finder" to duplicate alias ref1 to alias ref2 with replacing

【讨论】:

  • 对不起,这是我看到的第一个,但您仍然是我所需要的非常准确的答案
  • 实际上我似乎都遇到了错误 D: "error "Finder got an error: Can't set POSIX file \"/Users/MitchSimpson/Desktop/\" to POSIX file \"/Users/MitchSimpson/Documents/img.jpg\"." 编号 -10006 来自文件 "Macintosh HD:Users:MitchSimpson:Desktop:""
  • Finder 的副本有问题。它也不喜欢 posix 路径文件引用。我修改了脚本以首先将它们转换为基于冒号的路径。应该工作。
  • 我不知道这是否只是一个错误,但我似乎收到关于我的桌面别名“文件别名 Macintosh HD:Users:Mitc/Desktop/ 未找到”的错误。数字 -43
  • 对不起,我引入了一个错误,因为我使脚本更“花哨”,也将文件名分解为属性。我已经展开了代码,这样它现在又可以工作了。 (请确保您正确拼写“Mitc/Mitch”。)
【解决方案2】:

您必须使用 " & 告诉脚本停止读取目录并添加变量,所以结果如下:

tell application "Finder"
     duplicate POSIX file "/Users/" & variable & "/Documents/img.jpg" to POSIX file "/Users/" & variable & "/Desktop/" with replacing
end tell

(确保使用“set”命令来生成变量,EG。)

set variable to "text"

希望这有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2018-01-07
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多