简体   繁体   English

Applescript文件夹操作不起作用

[英]Applescript Folder Action not working

I wrote an Applescript that would monitor a folder for new input. 我写了一个Applescript,它将监视文件夹中是否有新输入。 On receiving the new item, it would open iTunes, create a playlist and add the new files into the playlist. 收到新项目后,它将打开iTunes,创建播放列表,然后将新文件添加到播放列表中。 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. 我已将该脚本放在/ Library / Scripts / Folder Action Scripts以及〜/ Library / Scripts / Folder Action Scripts中,但仍不会触发。 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 我正在运行Mac OS 10.8.4

In the future 在将来

  1. Test your code in the Applescript Editor, it helps a lot :) 测试中的AppleScript编辑器中的代码,它有很大帮助:)
  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. 如果您不使用Applescript编辑器,则要获取有用的消息,请在整个代码中使用display dialog条目,以查看进行的程度和发生的情况。

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. 因此,假设问题出在您的代码上,我将其重写为一个简单的Applescript,并使其在我的Snow Leopard(10.6.8)系统上工作。

It also runs ok from inside Automator as a Folder Action with the code in a Run Applescript Action item. 它也可以从Automator内部作为文件夹动作正常运行,并带有“运行Applescript动作”项中的代码。 Once saved from inside Automator it did activate when new files were added to the folder and run ok. 从Automator内部保存后,它确实会在将新文件添加到文件夹并运行ok时激活。

PS: Since you didn't provide any details, I had to guesstimate from your code what you were trying to do. PS:由于您未提供任何详细信息,因此我不得不从您的代码中猜测出您要尝试执行的操作。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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