1

此脚本与 iTunes 对话并创建 iTunes 库中所有文件的列表,但如果 iTunes 库很大,可能需要一段时间,我认为部分原因是它正在写入文件,我只是想知道是否这是写入文件的最有效方式

tell application "iTunes"
    set thePath to (POSIX file "/tmp/jaikoz_itunes_model.txt")
    set fileref to open for access (thePath) with write permission
    set eof fileref to 0
    set mainLibrary to library playlist 1
    repeat with nexttrack in (get every track of mainLibrary)
        if (class of nexttrack is file track) then
            try
                set trackname to name of nexttrack
                set loc to location of nexttrack
                set locpath to POSIX path of loc
                set persistid to persistent ID of nexttrack
                set nextline to trackname & "::" & locpath & "::" & persistid
                write nextline & "\n" as "utf8" to fileref  starting at eof
            end try
        end if
    end repeat
    close access fileref
end tell
return ""
4

2 回答 2

1

您可以使用批处理使脚本更快...

   set maxBatch to 1000
set thePathA to POSIX path of (path to desktop as text) & "jaikoz_itunes_model_noFormat.txt"
set thePathB to POSIX path of (path to desktop as text) & "jaikoz_itunes_model.txt"

    tell application "iTunes"
        set mainLibrary to library playlist 1

        script s
        property theTracks : get every track of mainLibrary
    end script

    set trackBatch to {}
    set batchCount to 0

    repeat with nexttrack in s's theTracks
        if (class of nexttrack is file track) then
            try
                set trackname to name of nexttrack
                set loc to location of nexttrack
                set locpath to POSIX path of loc
                set persistid to persistent ID of nexttrack
                set end of trackBatch to trackname & "::" & locpath & "::" & persistid & linefeed
                set batchCount to batchCount + 1
                if batchCount mod maxBatch = 0 then
                    set trackBatch to trackBatch as text
                    do shell script "echo " & quoted form of trackBatch & " >> " & quoted form of thePathA
                    set trackBatch to {}
                end if
            end try
        end if
    end repeat
end tell

if trackBatch ≠ {} then
    set trackBatch to trackBatch as text
    do shell script "echo " & quoted form of trackBatch & " >> " & quoted form of thePathA
    set trackBatch to {}
end if

do shell script "sed '/^$/ d' " & quoted form of thePathA & " > " & quoted form of thePathB & " ; rm " & quoted form of thePathA
于 2013-10-25T12:03:08.357 回答
-1

您可以测试两种方法:

(请注意,我只对此进行了部分测试)

--loading data into memory before writing:
tell application "iTunes"
    set thePath to (POSIX file "/tmp/jaikoz_itunes_model.txt")
    set mainLibrary to library playlist 1
    set fileData to ""
    repeat with nexttrack in (get every track of mainLibrary)
        if (class of nexttrack is file track) then
            try
                set trackname to name of nexttrack
                set loc to location of nexttrack
                set locpath to POSIX path of loc
                set persistid to persistent ID of nexttrack
                set nextline to trackname & "::" & locpath & "::" & persistid
                set fileData to (fileData & nextline & return)
            end try
        end if
    end repeat
    try
    set fileref to open for access (thePath) with write permission
    write fileData to fileref
    on error e
     close access fileref
     display dialog e
    end try
    close access fileref
end tell

或者,使用 do shell:

tell application "iTunes"
    --using raw POSIX style path below
    set mainLibrary to library playlist 1
    repeat with nexttrack in (get every track of mainLibrary)
        if (class of nexttrack is file track) then
            try
                set trackname to name of nexttrack
                set loc to location of nexttrack
                set locpath to POSIX path of loc
                set persistid to persistent ID of nexttrack
                set nextline to trackname & "::" & locpath & "::" & persistid
                --but caution: if there are any single quotes in the line of data, 
                -- you'll have a problem; you'd have to replace "'" with "\'" in it first
                do shell script ("echo " & (quoted form of nextline) & ">>/tmp/jaikoz_itunes_model.txt")
            end try
        end if
    end repeat
end tell
于 2013-10-25T10:12:34.993 回答