0

我正在编写一个程序,它不断地将桌面背景设置为在 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
4

2 回答 2

1

问题是外壳需要一个 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,您应该始终检查一个folderfile而不是一个文字字符串

    tell application "Finder"
        if not (exists folder folderName) then
            make new folder at desktop with properties {name:hiddenFile & directoryName}
        end if
    end tell
    

    (由于桌面文件夹是 Finder 的文件夹,您甚至可以省略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)。仅将受影响的术语包装在应用程序告诉块中。

于 2016-05-29T04:27:45.323 回答
0

尝试将 rm 命令中的“~”更改为“-”,使其看起来像这样:

do shell script "rm -rf " & folderName

这将 rf 视为参数而不是文件目录。

于 2016-05-29T00:38:09.833 回答