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

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

VagueExplanation
1#
VagueExplanation Published in 2018-01-09 21:50:44Z

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"
            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

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.”

vadian
2#
vadian Reply to 2018-01-10 18:20:03Z

The error occurs because you are going to write the file in the tell window block of Safari which cannot work.

I recommend to use a separate handler. Put the on writeFile handler outside of the on run handler. I added reliable error handling and the data are saved UTF-8 encoded.

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

and call it (replace the part of your code from delay 4 to the end)

                       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
VagueExplanation
3#
VagueExplanation Reply to 2018-01-10 18:22:33Z

This was fixed very easily by changing one line:

set newFile to POSIX file path1

to:

set newFile to (path1 as POSIX file)

But I don't know why. Seems like a bug since it worked without the variable. Please provide any details in the comments why this works over the original set newFile line.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO