使用下面的 2 个代码示例,我想选择一个文件夹并重命名其中的文件(一个 .pdf 文件)。 代码非常简单,但它返回错误 -10006。 我是 AppleScript 的新手。我在网站上发现了很多类似的请求。提供的解决方案对我不起作用...
on run
set Source to (choose folder with prompt "Pick the folder containing the images to process:") as alias
set newName to "new file name.pdf"
tell application "Finder"
try
set myFile to name of every file of Source as string
set name of myFile to newName as string
on error err
log ("Error in procedure: " & (err as text))
end try
end tell
end run
然后我尝试在第 7 行稍加修改(见下面的代码),现在错误代码是
error number -1728 from file "AD-3.HRYN.pdf"
-1728: 引用的对象不存在。这是一个运行时解析错误,例如当只有两个对象存在时试图引用第三个对象时。
on run
set Source to (choose folder with prompt "Pick the folder containing the images to process:") as alias
set newName to "new file name.pdf"
tell application "Finder"
try
set myFile to name of every file of Source as string
set name of file myFile to newName as string
on error err
log ("Error in procedure: " & (err as text))
end try
end tell
end run
控制台中的消息是:(程序错误:“AD-3.HRYN.pdf”的名称不可能是“新文件名.pdf”。)
我有文件夹的读/写权限。 你有什么想法吗?
据我了解,您的文件夹仅包含 1 个文件,您想要重命名该文件。由于您是初学者,我为您添加了解释性注释。当然,您可以删除它们。
on run
-- get source folder. The result of Choose Folder is already the Alias
-- so, no need additional coercion to alias
set sourceFolder to (choose folder with prompt "Pick the folder containing the images to process:")
-- set new name variable
set newName to "new file name.pdf"
tell application "Finder"
try
-- get 1st file's Finder reference
set myFile to file 1 of sourceFolder
-- rename it
set name of myFile to newName
on error err
-- err is a string already, no need additional coercion to string
log "Error in procedure: " & err
end try
end tell
end run