Applescript文件夹操作不起作用

时间:2014-11-09 21:12:23

标签: macos applescript

我写了一个Applescript,它会监视一个文件夹以获得新的输入。收到新项目后,它将打开iTunes,创建播放列表并将新文件添加到播放列表中。但是,当我通过文件夹操作设置将脚本作为文件夹操作附加并添加新项目时,没有任何反应。我已将脚本放在/ Library / Scripts / Folder Action Scripts以及〜/ Library / Scripts / Folder Action Scripts中,但它仍然没有触发。以下是我的代码:

global album_name
global artist_album

on adding folder items to this_folder after receiving added_items
--get the name of the added folder
repeat with x in added_items
    copy name of x as string to playlist_name
    set AppleScript's text item delimeters to "-"
    set delimited_playlist_name to every text item of playlist_name
    set artist_name to text item 1 of delimited_playlist_name
    set album_name to text item 2 of delimited_playlist_name
    set AppleScript's text item delimeters to ""
end repeat
tell "Finder" to activate "iTunes"
tell application "iTunes"
    if not (exists playlist album_name) then
        --make iTunes playlist
        make playlist with properties {name:album_name}
        --add the tracks to the iTunes playlist
        repeat with song in playlist_name
            add song to playlist album_name
            set album of song to album_name
            set artist of song to artist_name
            set comment of song to ""
            set composer of song to ""
            set grouping of song to ""
            set description of song to ""
            set long description of song to ""
            set (volume adjustment) of song to 100
        end repeat
        set view of browser window to album_name
    end if
end tell
end adding folder items to

我正在运行Mac OS 10.8.4

1 个答案:

答案 0 :(得分:0)

将来

  1. 在Applescript编辑器中测试您的代码,它可以帮助很多:)
  2. 如果您不使用Applescript编辑器来获取有用的消息,请在整个代码中使用display dialog条目来查看它的进展情况和发生的情况。
  3. 因此,假设问题出在您的代码中,我将其重写为一个简单的Applescript并将其用于我的Snow Leopard(10.6.8)系统。

    它也可以从Automator内部作为文件夹操作运行,其中包含Run Applescript Action项目中的代码。从Automator内部保存后,当新文件添加到文件夹并运行正常时它会激活。

    PS:由于你没有提供任何细节,我不得不从你的代码中猜出你想要做什么。它远非最佳,但它会让你去...

    HTH

    # remove this line for Applscript Action in Automator...
    set input to (choose file with multiple selections allowed)
    
    set playlist_name to {}
    
    #display dialog "Delimeter set to -"
    set AppleScript's text item delimiters to "-"
    
    repeat with x in input
    
        #display dialog "Item:  " & (x as text)
    
        tell application "Finder"
            set fl to name of x
            display dialog "Filename: " & fl
        end tell
    
        # bring back focus to the editor ## TESTING ONLY
        activate me
    
        # append item to the list       
        copy (fl as text) to the end of playlist_name
        display dialog "Playlist count: " & length of playlist_name
    
        set artist_name to text item 1 of fl
        display dialog "Artist: " & artist_name
    
        set album_name to text item 2 of fl
        display dialog "Album: " & album_name
    
    end repeat
    
    #display dialog "Activating iTunes..."
    #activate "iTunes"
    #display dialog "iTunes activated!"
    
    tell application "iTunes"
        if not (exists playlist album_name) then
            display dialog "Making new playlist: " & album_name
            make playlist with properties {name:album_name}
    
            display dialog "Changing view..."
            set view of browser window 1 to playlist album_name
    
            # add the tracks to the iTunes playlist
            repeat with song in input
    
                display dialog "Adding: " & song
                set newtrack to (add song to playlist album_name)
    
                set album of newtrack to album_name
                set artist of newtrack to artist_name
                set comment of newtrack to ""
                set composer of newtrack to ""
                set grouping of newtrack to ""
                set description of newtrack to ""
                set long description of newtrack to ""
                set (volume adjustment) of newtrack to 100
            end repeat
        else
            display dialog "iTunes playlist already exists!"
        end if
    end tell