0

我对 AppleScript 几乎一无所知,可以提供一些帮助。

在运行时,我的广播播放系统 (MegaSeg) 将当前播放的曲目的详细信息写入一个名为“NowPlaying”的文本文件,该文件仅包含以下内容,每个内容都在自己的行中且不带方括号:

Title: [title]
Artist: [artist]
Album: [album]
Time: [time in some format or other]

我想将它传递给我的广播流媒体 LadioCast,它可以通过 AppleScript 进行寻址,以便它将有关正在播放的内容的信息发送到流媒体服务器。MegaSeg 不支持 Applescript 调用,例如“告诉应用程序“MegaSeg”...将 trackName 设置为当前曲目的名称”,所以我不能那样做。我不知道该怎么做。

如果我们想象我能够获取该信息,我会这样做:

set lastName to ""
set lastArtist to ""
set lastAlbum to ""
repeat

    ** insert missing file reading section here
    ** in the following, "title", "artist" and "album" are from the text file **
    set trackName to title
    set trackArtist to artist
    set trackAlbum to album
    ** end of missing section

    if trackName is not lastName and trackArtist is not lastArtist and trackAlbum is not lastAlbum then
        set lastName to trackName
        set lastArtist to trackArtist
        set lastAlbum to trackAlbum
        tell application "LadioCast"
            set metadata song to trackName & " – " & trackArtist & " – " & trackAlbum
        end tell
    end if
    delay 15
end repeat

提前致谢。——理查德·E

===

我尝试了@DigiMonk 的建议,它们非常有帮助,但并非所有这些都有效。首先,文件在特定位置,而不是在桌面上;其次,我根本无法让“trim_line”工作 - 我收到“脚本不理解 trim_line 消息”。但是文件中的文本似乎已经被修剪了。

下面的脚本几乎可以工作;从 Applescript 编辑器运行时,它似乎确实将 Title、Artist 和 Album 放入变量中。但是,当我尝试从 LadioCast 运行它时,我收到 EOF -39 错误。更重要的是,一旦我运行它,MegaSeg 就会永远停止更新文件。我大概锁定了文件以从中读取并停止 MegaSeg 写入它。我该如何避免这种情况?

set lastName to ""
set lastArtist to ""
set lastAlbum to ""
set lastTime to ""
set trackName to ""
set trackArtist to ""
set trackAlbum to ""
set sourcePath to ""

repeat

    set sourcePath to open for access file "Library:MegaSeg User Data:Logs:Logs for MegaSeg System (4):NowPlaying"
    set thisText to read sourcePath as text
    close access file "Library:MegaSeg User Data:Logs:Logs for MegaSeg System (4):NowPlaying"

    set the paragraphList to every paragraph of thisText

    repeat with i from 1 to number of items in paragraphList
        set thisItem to item i of paragraphList

        if thisItem starts with "Title:" then

            set x to the offset of "Title:" in thisItem
            set trackName to (text (x + 6) thru -1 of thisItem)

        else if thisItem starts with "Artist:" then
            set x to the offset of "Artist:" in thisItem
            set trackArtist to (text (x + 7) thru -1 of thisItem)

        else if thisItem starts with "Album:" then
            set x to the offset of "Album:" in thisItem
            set trackAlbum to (text (x + 6) thru -1 of thisItem)

        end if

    end repeat

    if trackName is not lastName and trackArtist is not lastArtist and trackAlbum is not lastAlbum then
        set lastName to trackName
        set lastArtist to trackArtist
        set lastAlbum to trackAlbum
        tell application "LadioCast"
            set metadata song to trackName & " – " & trackArtist & " – " & trackAlbum
        end tell
    end if
    delay 15

end repeat
4

1 回答 1

0

可能这会有所帮助:(使用 NowPlaying.txt 文件的路径提供 sourcePath)

set sourcePath to (path to desktop) & "NowPlaying.txt" as text

set thisText to my readFile(sourcePath)

set the paragraphList to every paragraph of thisText

set lastName to ""
set lastArtist to ""
set lastAlbum to ""
set lastTime to ""

repeat with i from 1 to number of items in paragraphList
    set thisItem to item i of paragraphList
    --log thisItem

    if thisItem starts with "Title:" then

        set x to the offset of "Title:" in thisItem
        set y to (text (x + 6) thru -1 of thisItem)
        set lastName to my trim_line(y, " ", 2)

    else if thisItem starts with "Artist:" then
        set x to the offset of "Artist:" in thisItem
        set y to (text (x + 7) thru -1 of thisItem)
        set lastArtist to my trim_line(y, " ", 2)

    else if thisItem starts with "Album:" then
        set x to the offset of "Album:" in thisItem
        set y to (text (x + 6) thru -1 of thisItem)
        set lastAlbum to my trim_line(y, " ", 2)

    else if thisItem starts with "Time:" then
        set x to the offset of "Time:" in thisItem
        set y to (text (x + 5) thru -1 of thisItem)
        set lastTime to my trim_line(y, " ", 2)

    end if


end repeat

log "lastName = '" & lastName & "'"
log "lastArtist = '" & lastArtist & "'"
log "lastAlbum = '" & lastAlbum & "'"



on readFile(thisFile)
    set thisFile to thisFile as text
    if thisFile is "" then return ""
    try
        set fi to open for access file the thisFile
        set myData to read fi as text
        close access file the thisFile
        return myData
    on error
        try
            close access file the thisFile
            return ""
        end try
    end try
end readFile

on trim_line(this_text, trim_chars, trim_indicator)
    -- 0 = beginning, 1 = end, 2 = both
    set x to the length of the trim_chars
    -- TRIM BEGINNING
    if the trim_indicator is in {0, 2} then
        repeat while this_text begins with the trim_chars
            try
                set this_text to characters (x + 1) thru -1 of this_text as string
            on error
                -- the text contains nothing but the trim characters
                return ""
            end try
        end repeat
    end if
    -- TRIM ENDING
    if the trim_indicator is in {1, 2} then
        repeat while this_text ends with the trim_chars
            try
                set this_text to characters 1 thru -(x + 1) of this_text as string
            on error
                -- the text contains nothing but the trim characters
                return ""
            end try
        end repeat
    end if
    return this_text
end trim_line
于 2013-11-08T17:16:44.497 回答