无法通过 AppleScript 删除目录
Can't delete directory via AppleScript
我正在编写一个程序,不断将桌面背景设置为当前在 iTunes 中播放的歌曲的专辑封面,为此我将专辑封面写入一个具有随机 ID 号的文件。这意味着每次桌面都会更改为不同的文件。
当然,我希望能够在使用完这些文件后删除所有这些文件,因此我尝试使用 do shell script "rm ~rf " & folderName
删除包含图像的整个文件夹。
但是每次我 运行 我的脚本甚至在终端中输入完整目录时,我都会收到错误消息:
"rm: ~rf: 没有那个文件或目录
rm: Macintosh: 没有那个文件或目录
rm: HD:Users:BenClose:Desktop:iTunesWallpaper: 没有那个文件或目录
下面是我的整个程序的代码,我不知道哪里出了问题,所以任何帮助将不胜感激,因为我希望任何想要它的人都能够使用这个程序。
screenNum
指的是正在更改的桌面。
hiddenFile
指创建的文件是否隐藏。 "."
if true and ""
if false(在文件名开头添加句点将使其隐藏)。
基本上,发生的事情是桌面背景被设置为专辑封面 99 次,之后我希望能够删除所有 99 个文件并让该过程重新开始。
错误发生在脚本的最后,它试图删除图像文件夹。
set screenNum to 2
set directoryName to "iTunesWallpaper"
set fileExt to "jpg"
set hiddenFile to ""
set repeatTrue to 1
set loopLimit to 99
set looped to 0
set folderName to ((path to desktop) as text) & hiddenFile & directoryName
repeat while repeatTrue is 1
tell application "Finder"
if (exists folderName) is not true then
make new folder at (path to desktop) as text with properties {name:hiddenFile & directoryName}
end if
end tell
set randID to screenNum
set randLoop to 0
repeat while randLoop is not 9
set randNum to (random number from 0 to 9) as text
set randID to randID & randNum
set randLoop to randLoop + 1
end repeat
tell application "System Events"
set fileName to {((path to desktop) as text) & hiddenFile & directoryName & ":" & randID & "." & fileExt}
set changeDesktop to 0
if process "iTunes" exists then
tell application "iTunes"
if (player state is not stopped) then
if exists artworks of current track then
set changeDesktop to 1
-- get the raw bytes of the artwork into a var
tell application "iTunes" to tell artwork 1 of current track
set srcBytes to raw data
end tell
-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile
end if
end if
end tell
if changeDesktop = 1 then
if exists desktop screenNum then
tell desktop screenNum
set picture to fileName
end tell
end if
end if
end if
end tell
set looped to looped + 1
if looped is loopLimit then
do shell script "rm ~rf " & folderName
set looped to 0
end if
end repeat
尝试将 rm 命令中的“~”更改为“-”,使其看起来像这样:
do shell script "rm -rf " & folderName
这将 rf 视为参数而不是文件目录。
问题是 shell 需要 POSIX 路径(斜线分隔)
do shell script "rm -rf " & quoted form of POSIX path of folderName
使脚本更健壮的一些其他问题/注意事项:
path to
有一个参数 as text
。用它代替强制
set folderName to (path to desktop as text) & hiddenFile & directoryName
在 Finder 中有一个 属性 desktop
,您应该始终检查 folder
或 file
而不是文字字符串
tell application "Finder"
if not (exists folder folderName) then
make new folder at desktop with properties {name:hiddenFile & directoryName}
end if
end tell
(由于桌面文件夹是 Finder 的 root 文件夹,您甚至可以省略 at desktop
)。
randID
将是一个列表,因为 screenNum
是一个整数,但随后您要添加字符串,因此将 randID
强制转换为文本。
set randID to screenNum as text
不需要用于创建随机数的 while 表达式。 AppleScript 可以重复特定次数。
set randID to screenNum
repeat 9 times
set randNum to (random number from 0 to 9) as text
set randID to randID & randNum
end repeat
fileName
也是一个无意的列表。去掉大括号。
set fileName to (path to desktop as text) & hiddenFile & directoryName & ":" & randID & "." & fileExt
使用 write
命令,您 必须 捕获潜在的错误,否则文件可能无法关闭。添加 try - on error
块可确保可靠地关闭所有文件。
try
-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile
on error
try
close access file fileName
end try
end try
与其询问 System Events
进程是否 运行,不如询问应用程序本身。
if application "iTunes" is running then
最后但并非最不重要的一点是避免大型嵌套应用程序告诉块(如 System Events
块)。仅将受影响的术语包装在应用程序告诉块中。
我正在编写一个程序,不断将桌面背景设置为当前在 iTunes 中播放的歌曲的专辑封面,为此我将专辑封面写入一个具有随机 ID 号的文件。这意味着每次桌面都会更改为不同的文件。
当然,我希望能够在使用完这些文件后删除所有这些文件,因此我尝试使用 do shell script "rm ~rf " & folderName
删除包含图像的整个文件夹。
但是每次我 运行 我的脚本甚至在终端中输入完整目录时,我都会收到错误消息:
"rm: ~rf: 没有那个文件或目录 rm: Macintosh: 没有那个文件或目录 rm: HD:Users:BenClose:Desktop:iTunesWallpaper: 没有那个文件或目录
下面是我的整个程序的代码,我不知道哪里出了问题,所以任何帮助将不胜感激,因为我希望任何想要它的人都能够使用这个程序。
screenNum
指的是正在更改的桌面。
hiddenFile
指创建的文件是否隐藏。 "."
if true and ""
if false(在文件名开头添加句点将使其隐藏)。
基本上,发生的事情是桌面背景被设置为专辑封面 99 次,之后我希望能够删除所有 99 个文件并让该过程重新开始。
错误发生在脚本的最后,它试图删除图像文件夹。
set screenNum to 2
set directoryName to "iTunesWallpaper"
set fileExt to "jpg"
set hiddenFile to ""
set repeatTrue to 1
set loopLimit to 99
set looped to 0
set folderName to ((path to desktop) as text) & hiddenFile & directoryName
repeat while repeatTrue is 1
tell application "Finder"
if (exists folderName) is not true then
make new folder at (path to desktop) as text with properties {name:hiddenFile & directoryName}
end if
end tell
set randID to screenNum
set randLoop to 0
repeat while randLoop is not 9
set randNum to (random number from 0 to 9) as text
set randID to randID & randNum
set randLoop to randLoop + 1
end repeat
tell application "System Events"
set fileName to {((path to desktop) as text) & hiddenFile & directoryName & ":" & randID & "." & fileExt}
set changeDesktop to 0
if process "iTunes" exists then
tell application "iTunes"
if (player state is not stopped) then
if exists artworks of current track then
set changeDesktop to 1
-- get the raw bytes of the artwork into a var
tell application "iTunes" to tell artwork 1 of current track
set srcBytes to raw data
end tell
-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile
end if
end if
end tell
if changeDesktop = 1 then
if exists desktop screenNum then
tell desktop screenNum
set picture to fileName
end tell
end if
end if
end if
end tell
set looped to looped + 1
if looped is loopLimit then
do shell script "rm ~rf " & folderName
set looped to 0
end if
end repeat
尝试将 rm 命令中的“~”更改为“-”,使其看起来像这样:
do shell script "rm -rf " & folderName
这将 rf 视为参数而不是文件目录。
问题是 shell 需要 POSIX 路径(斜线分隔)
do shell script "rm -rf " & quoted form of POSIX path of folderName
使脚本更健壮的一些其他问题/注意事项:
path to
有一个参数as text
。用它代替强制set folderName to (path to desktop as text) & hiddenFile & directoryName
在 Finder 中有一个 属性
desktop
,您应该始终检查folder
或file
而不是文字字符串tell application "Finder" if not (exists folder folderName) then make new folder at desktop with properties {name:hiddenFile & directoryName} end if end tell
(由于桌面文件夹是 Finder 的 root 文件夹,您甚至可以省略
at desktop
)。randID
将是一个列表,因为screenNum
是一个整数,但随后您要添加字符串,因此将randID
强制转换为文本。set randID to screenNum as text
不需要用于创建随机数的 while 表达式。 AppleScript 可以重复特定次数。
set randID to screenNum repeat 9 times set randNum to (random number from 0 to 9) as text set randID to randID & randNum end repeat
fileName
也是一个无意的列表。去掉大括号。set fileName to (path to desktop as text) & hiddenFile & directoryName & ":" & randID & "." & fileExt
使用
write
命令,您 必须 捕获潜在的错误,否则文件可能无法关闭。添加try - on error
块可确保可靠地关闭所有文件。try -- write to file set outFile to open for access file fileName with write permission -- truncate the file set eof outFile to 0 -- write the image bytes to the file write srcBytes to outFile close access outFile on error try close access file fileName end try end try
与其询问
System Events
进程是否 运行,不如询问应用程序本身。if application "iTunes" is running then
最后但并非最不重要的一点是避免大型嵌套应用程序告诉块(如
System Events
块)。仅将受影响的术语包装在应用程序告诉块中。