当我在posix路径中使用变量时,忽略其余的AppleScript

时间:2018-01-09 21:50:45

标签: variables applescript posix

我在Automator中使用AppleScript复制页面的源并将其保存到文件中。出于某种原因,当我在posix路径中使用变量(titleVal)时,我的循环中的其余代码将被忽略,包括永远不会被写入的文件。

我之前用完整的AppleScript更新了代码,以防它与我之前的几行有关。我按照以下顺序使用Automator和指定的Finder项目:“urlList.txt”和fileList.txt“。

    on run {input, parameters}
        set updateCount to 0
        read (item 1 of input)
        set ps to paragraphs of the result
        set tot to count ps
        set TLFile to (("Users:Admin:Desktop:download captions:") as text) & "fileList.txt"
        set TLLines to paragraphs of (read file TLFile as «class utf8»)
        tell application "Safari"
            reopen
            activate
        end tell
        repeat with i from 1 to tot
            set p to item i of ps
            if p is not "" then
                try
                    tell application "Safari"
                        tell front window

                            set r to make new tab with properties {URL:"https://www.youtube.com/timedtext_editor?v=" & p & "&lang=en&name=&kind=&contributor_id=0&bl=vmp&action_view_track=1"}


                            set current tab to r

                            set titleVal to item i of TLLines
                            set updateCount to updateCount + 1
                            do shell script "echo The value: " & updateCount



                            delay 2

                            do JavaScript "document.getElementById('movie_player').outerHTML = ''" in current tab

                            do JavaScript "document.getElementById('creator-page-sidebar').outerHTML = ''" in current tab

                            do JavaScript "document.getElementById('footer').outerHTML = ''" in current tab

                            delay 3

                            do JavaScript "document.getElementsByClassName('yt-uix-button yt-uix-button-size-default yt-uix-button-default action-track-button flip yt-uix-menu-trigger')[0].click()" in current tab

                            delay 1



                            do JavaScript "document.getElementById('aria-menu-id-2').getElementsByTagName('ul')[0].getElementsByTagName('li')[5].getElementsByTagName('a')[0].click()" in current tab

                            delay 4

-- using a variable in path1 is where it screws up. try changing it to another variable value and it will have the same effect.
                            set myString to source of current tab
                            set path1 to "/Users/Admin/Desktop/download captions/downloadedCaptions/" & titleVal & ".srt"
                            say path1
                            set newFile to POSIX file path1

                            --set newFile to POSIX file "/Users/Admin/Desktop/download captions/downloadedCaptions/test.xml.srt"

                            open for access newFile with write permission
                            write myString to newFile
                            close access newFile

                            -- i have exit repeat here to only test the first loop
                            exit repeat 

                        end tell
                    end tell
                end try
            end if
        end repeat
    end run

没有变量可以正常工作,但我需要变量来使脚本在循环中正常工作。我检查了var的值。我还尝试了“& quoted形式的titleVal&”。

更新:当我删除try / end尝试按照建议获取错误时,错误是: “运行AppleScript”操作遇到错误:“Safari收到错误:无法获取POSIX文件”/窗口1的用户/管理员/桌面/下载字幕/ downloadedCaptions / test.srt“。

2 个答案:

答案 0 :(得分:1)

发生错误是因为您要在Safari的tell window块中写入无法正常工作的文件。

我建议使用单独的处理程序。将on writeFile处理程序放在on run处理程序之外。我添加了可靠的错误处理,数据保存为UTF-8编码。

on writeFile(theData, fileName)
    set newFile to "/Users/Admin/Desktop/download captions/downloadedCaptions/" & fileName & ".srt"
    try
        set fileDescriptor to open for access newFile with write permission
        write theData to fileDescriptor as «class utf8»
        close access fileDescriptor
    on error
        try
            close access newFile
        end try
    end try
end writeFile

并调用它(将代码的一部分从delay 4替换到结尾)

                       delay 4

     -- using a variable in path1 is where it screws up. try changing it to another variable value and it will have the same effect.
                        set myString to source of current tab
                        my writeFile(myString, titleVal)
                        exit repeat 

                    end tell
                end tell
            end try
        end if
    end repeat
 end run

答案 1 :(得分:0)

通过更改一行非常容易解决这个问题:

set newFile to POSIX file path1

为:

set newFile to (path1 as POSIX file)

但我不知道为什么。看起来像一个bug,因为它没有变量。请在评论中提供任何详细信息,以解释为什么它适用于原始set newFile行。