我正在使用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
答案 0 :(得分:1)
选择文件后,尝试以下操作:
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
答案 1 :(得分:1)
如果当前要在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
答案 2 :(得分:0)
不要可以通过AppleScript本地命令的GUI脚本操作。
使用 System Events
重命名和移动文件:
tell application id "sevs"
set base_folder to folder "~/Desktop/"
set f_old to "myOldFile.csv" -- file to move (original name)
set f_new to "myFile.csv" -- new file name
set dir to "TestFolder" -- destination folder
tell the base_folder
set the name of the file named f_old to f_new
move the file named f_new to the folder named dir
end tell
end tell
使用 Finder
重命名和移动文件:
tell application id "MACS" --OR: "com.apple.Finder"
set base_folder to folder (POSIX file "/Users/joe/Desktop/")
set f_old to "myOldFile.csv" -- file to move (original name)
set f_new to "myFile.csv" -- new file name
set dir to "TestFolder" -- destination folder
tell the base_folder
set the name of the file named f_old to f_new
move the file named f_new to the folder named dir with replacing
end tell
end tell
从本质上讲,已经按照我的方法分解了路径,除了分配给变量base_folder
的值(需要为其设置 Finder
)之外,两个脚本都是相同的明确知道它正在处理posix表示法中的文件路径。另外,在基本文件夹是桌面文件夹的特定情况下, Finder
和 System Events
都应该能够理解这一点:>
set base_folder to the path to the desktop folder