0

デスクトップの背景を iTunes で現在再生中の曲のアルバム アートに継続的に設定するプログラムを作成しています。そのために、ランダムな ID 番号を持つファイルにアートワークを書き込んでいます。これは、デスクトップが毎回別のファイルに変更されることを意味します。

もちろん、使用後にこれらのファイルをすべて削除できるようにしたいのでdo shell script "rm ~rf " & folderName、画像を含むフォルダー全体を削除するために使用しようとしています。

ただし、スクリプトを実行するたびに、またはターミナルに完全なディレクトリを入力するたびに、次のエラーが表示されます。

「rm: ~rf: そのようなファイルまたはディレクトリはありません rm: Macintosh: そのようなファイルまたはディレクトリはありません rm: HD:Users:BenClose:Desktop:iTunesWallpaper: そのようなファイルまたはディレクトリはありません」

以下は私のプログラム全体のコードです。何がどこで間違っているのかわかりません。そのため、このプログラムを使用できるようにしたい人なら誰でも使用できるようにしたいので、どんな助けでも大歓迎です。

screenNumどのデスクトップが変更されているかを示します。

hiddenFile作成されたファイルが非表示になるかどうかを示します。"."true の""場合と 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 にはプロパティがあり、常にリテラル文字列ではなくordesktopをチェックする必要があります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フォルダであるため、省略してもかまいません)。

  • randIDscreenNumは整数であるためリストになりますが、文字列を追加しているため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
    
  • 最後になりましたが、大規模なネストされたアプリケーションの Tell ブロッ​​ク (ブロックのようなSystem Eventsもの) は避けてください。影響を受ける用語のみを application tell ブロッ​​クにラップします。

于 2016-05-29T04:27:45.323 に答える
0

次のように、rm コマンドの「~」を「-」に変更してみてください。

do shell script "rm -rf " & folderName

これは、rf をファイル ディレクトリではなく引数として扱います。

于 2016-05-29T00:38:09.833 に答える