Home Rest of AppleScript is ignored when I use a variable in posix path
Reply: 0

Rest of AppleScript is ignored when I use a variable in posix path

user3992 Published in July 23, 2018, 1:35 pm

I'm using AppleScript in Automator to copy a page's source and save it to a file. For some reason when I use a variable (titleVal) in the posix path, the rest of my code in my loop is ignored, including the file that never gets written.

I updated the code before with my full AppleScript in case it has to do with more than the few lines I had before. I'm using Automator with specified Finder items in this order: "urlList.txt" and 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"
        end tell
        repeat with i from 1 to tot
            set p to item i of ps
            if p is not "" then
                    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

Without a variable works fine, but I need the variable to make the script work properly in a loop. I've checked the value of the var. I also tried "& quoted form of titleVal &".

Update: When I remove the try/end try as suggested to get the error, the error is: The action “Run AppleScript” encountered an error: “Safari got an error: Can’t get POSIX file "/Users/Admin/Desktop/download captions/downloadedCaptions/test.srt" of window 1.”

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324289 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO