更改文件名后,使用AppleScript移动文件无效

更改文件名后,使用AppleScript移动文件无效,applescript,Applescript,我正在使用AppleScript重命名文件并将其移动到文件夹。这是使用语音命令执行的。它不会将文件移动到文件夹中,而是按Enter键,将文件重命名为“myFile”,然后再次按Enter键 但是,如果我再次执行此操作,或者如果文件名为“myFile”,则它将正常工作。我认为移动文件的代码不知道,或者文件名没有更新。我不知道怎么解决这个问题。AppleScript不是我喜欢的 tell application "System Events" key code 36 keystrok

我正在使用AppleScript重命名文件并将其移动到文件夹。这是使用语音命令执行的。它不会将文件移动到文件夹中,而是按Enter键,将文件重命名为“myFile”,然后再次按Enter键

但是,如果我再次执行此操作,或者如果文件名为“myFile”,则它将正常工作。我认为移动文件的代码不知道,或者文件名没有更新。我不知道怎么解决这个问题。AppleScript不是我喜欢的

tell application "System Events"
    key code 36
    keystroke "myFile"
    key code 36
end tell

tell application "Finder"
    move POSIX file "/Users/joe/Desktop/myFile.csv" to POSIX file "/Users/joe/Desktop/TestFolder" with replacing
end tell

不要编写可以通过AppleScript本机命令的GUI脚本操作

要使用
系统事件重命名和移动文件

要使用
Finder
重命名和移动文件,请执行以下操作:

从本质上讲,正如我所做的那样分解了路径,除了分配给变量
base_folder
的值之外,这两个脚本是相同的,对于变量
Finder
,需要明确地知道它正在处理posix表示法中的文件路径。或者,在基本文件夹是桌面文件夹的特定情况下,
Finder
System Events
都应该能够理解这一点:

set base_folder to the path to the desktop folder

选择文件后,请尝试以下操作:

tell application "Finder"

    set itemlist to the selection
    set theFile to (item 1 of itemlist) as alias
    set name of theFile to "myFile.csv"

    move POSIX file "/Users/joe/Desktop/myFile.csv" to POSIX file "/Users/joe/Desktop/TestFolder" with replacing

end tell

如果要移动的文件当前已在Finder中选中,并且您希望能够设置新名称…此解决方案可能适用于您

property moveToFolder : (path to desktop as text) & "TestFolder"

set newName to text returned of (display dialog "Name Your File" default answer ¬
    "myFile.csv" buttons {"Cancel", "OK"} ¬
    default button 2 cancel button 1 with title "Name Your File")

tell application "Finder"
    set originalFile to item 1 of (get selection) as alias
    set theFile to (move originalFile to alias moveToFolder) as alias
    if (exists of alias (moveToFolder & ":" & newName)) then ¬
        delete alias (moveToFolder & ":" & newName)
    set name of theFile to newName
end tell

这是否可能与Finder中选定的内容一起工作?如果是这样的话,那么编写UI脚本而不是仅仅告诉查找器或系统事件进行移动和重命名有什么特别的原因吗?我没有AppleScript方面的经验。我不太清楚你在问我什么。我目前没有电脑,因此如果上面的脚本包含任何小错误,请提前道歉,但我会在我拿回电脑后返回查看。查找者甚至理解
将base\u文件夹设置为desktop
。桌面文件夹是查找器的根文件夹。@vadian,是的,确实如此。该段的主题是首先指出两个脚本中不同的一行,从而保持它们彼此之间的独特性;然后提供一个替代方案,将两个脚本统一到各自
tell
块中的相同代码行中。但是,您刚刚在我的记忆中发现,
桌面文件夹
不是
查找程序
中的
文件夹
,因此这不起作用;而
系统事件
中的
桌面
不是
文件夹
。但是,我相信这两种方法都适用于路径,当然,它会返回一个别名,所以这就是我想要的。感谢您的回复!第一个示例更改名称,但不将文件移动到文件夹中。第二个例子是不为我做任何事。有件重要的事我应该提一下。文件名并不总是相同的。这就是我改变它的原因。
tell application "Finder"

    set itemlist to the selection
    set theFile to (item 1 of itemlist) as alias
    set name of theFile to "myFile.csv"

    move POSIX file "/Users/joe/Desktop/myFile.csv" to POSIX file "/Users/joe/Desktop/TestFolder" with replacing

end tell
property moveToFolder : (path to desktop as text) & "TestFolder"

set newName to text returned of (display dialog "Name Your File" default answer ¬
    "myFile.csv" buttons {"Cancel", "OK"} ¬
    default button 2 cancel button 1 with title "Name Your File")

tell application "Finder"
    set originalFile to item 1 of (get selection) as alias
    set theFile to (move originalFile to alias moveToFolder) as alias
    if (exists of alias (moveToFolder & ":" & newName)) then ¬
        delete alias (moveToFolder & ":" & newName)
    set name of theFile to newName
end tell