更改文件名后,使用 AppleScript 移动文件不起作用
Moving a file using AppleScript not working after changing the file name
我正在使用 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 脚本操作。
重命名和移动文件使用 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
选择文件后,试试这个:
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
我正在使用 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 脚本操作。
重命名和移动文件使用 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
选择文件后,试试这个:
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