【问题标题】:Get path to main iTunes library and play a song from it获取主 iTunes 资料库的路径并从中播放歌曲
【发布时间】:2014-09-09 20:21:34
【问题描述】:

我目前有一个 Applescript,可让您输入歌曲并播放。

这里是:

    set userInput to text returned of (display dialog "Type something" default answer "")
if userInput contains "Play " then
    set {TID, text item delimiters} to {text item delimiters, {"Play "}}
    if length of userInput is greater than or equal to 2 then set resultString to text item 2 of userInput
    set text item delimiters to TID
    set playSong to (resultString as string)
    tell application "iTunes"
        set mySongs to every track of library playlist 1 whose name is playSong
        repeat with aSong in mySongs
        play aSong
        end repeat

        if (count of mySongs) = 0 then
            say "Song not found"
        end if
    end tell
end if

基本上,我需要获取主 iTunes 库的路径并从中播放歌曲。目前,要搜索歌曲,iTunes 必须打开。如果它找不到歌曲,它就会保持打开状态。我想搜索实际的 iTunes 目录来制作它,所以如果 iTunes 找不到歌曲,它就不会打开

我不知道该怎么做。

谢谢

【问题讨论】:

    标签: macos applescript itunes


    【解决方案1】:

    这是在“iTunes Library.xml”文件中搜索的脚本。

    set XMLFile to my get_iTunes_Library_xml()
    set userInput to text returned of (display dialog "Type something" default answer "Play ")
    if userInput begins with "Play " and length of userInput > 5 then
        set playSong to text 6 thru -1 of userInput
        set searchString to "<key>Name<\\/key><string>" & playSong & "<\\/string>" --  to match exact name 
        if (my searchTrackName(searchString, XMLFile)) is not "" then
            tell application "iTunes" to play (tracks whose name is playSong)
        else
            say "Song not found"
        end if
    end if
    
    on searchTrackName(t, f) -- search in iTunes Library.xml file
        if "&" is in t then set t to do shell script "sed  \"s/&/&#38;/g\" <<<" & quoted form of t -- to replace "&" by "&#38;"
        try -- return a count of matching lines
            return do shell script "grep -c -m1 -i " & (quoted form of t) & " " & f -- "-i" equal case insensitive, "-m 1" to exit at first match
        end try
        return ""
    end searchTrackName
    
    on get_iTunes_Library_xml() -- get the path
        do shell script "defaults read com.apple.iApps iTunesRecentDatabases | sed -En 's:^ *\"(.*)\"$:\\1:p' |/usr/bin/perl -MURI -e 'print URI->new(<>)->file;'"
        return quoted form of the result
    end get_iTunes_Library_xml
    

    com.apple.iApps.plist”文件包含您的 iTunes 库的路径(如果您有多个),脚本获取“iTunes 库”的路径.xml" 当前库的文件。

    【讨论】:

    • 谢谢!这正是我所需要的。
    【解决方案2】:

    不是一个完成的解决方案,但也许是一个起点。

    我没有找到自动获取 iTunes 资料库路径的方法。 使用 StandardAdditions,您可以通过

    获取主文件夹中主音乐文件夹的路径
    set searchPath to POSIX path of music folder
    

    你可以手动设置路径,像这样:

    set searchPath to quoted form of "/Users/USERNAME/Music/iTunes/iTunes Music/"
    

    要在没有 iTunes 的情况下搜索音乐文件,我使用 shell 命令“mdfind”

    do shell script "mdfind -count -onlyin PATH SEARCHSTRING"
    

    使用 count-Flag 我们可以得到匹配的总数。如果您想查看 mdfind 找到了什么,请忽略 count-Flag。

    tell application "System Events"
        set userInput to text returned of (display dialog "Search for music" default answer "madonna")
        set searchPath to POSIX path of music folder
    end tell
    
    set shellCommand to "mdfind -count -onlyin " & searchPath & " " & quoted form of userInput        
    set searchResult to (do shell script shellCommand) as number
    
    if searchResult = 0 then
        say "Song not found"
    else if searchResult >= 1 then
        say "Found some songs"
    end if
    

    使用 mdfind 您可以在元数据中搜索,例如,您可以只搜索 MP3 文件:

    mdfind "kMDItemContentType=='public.mp3'"
    

    【讨论】:

    • 这也很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多