简体   繁体   中英

Applescript Folder Action not working

I wrote an Applescript that would monitor a folder for new input. On receiving the new item, it would open iTunes, create a playlist and add the new files into the playlist. However, when I attach the script as a Folder Action via Folder Action Setup and add a new item, nothing happens. I have placed the script inside /Library/Scripts/Folder Action Scripts as well as ~/Library/Scripts/Folder Action Scripts but it still doesn't fire. Here is my code below:

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

I'm running Mac OS 10.8.4

In the future

  1. Test your code in the Applescript Editor, it helps a lot :)
  2. If you dont use Applescript Editor, to get useful messages, use display dialog entries throughout your code to see how far it goes and what is going on.

So, assuming that the problem is with your code, I rewrote it as an simple Applescript and got it to work on my Snow Leopard (10.6.8) system.

It also runs ok from inside Automator as a Folder Action with the code in a Run Applescript Action item. Once saved from inside Automator it did activate when new files were added to the folder and run ok.

PS: Since you didn't provide any details, I had to guesstimate from your code what you were trying to do. It is far from optimal but it will get you going...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM