Apple Script: Opening urls from Plain Text and Running Separate Actions Depending on Formatting

I'm new to the forums, applescript and automator. So everything this topic includes basically. I recently bought some online learning courses which I would like to be able to view offline on my commute into work.


My process so far includes:

  1. Converting all links to a plain text file in text edit.
  2. Run an automator workflow to open a url in safari, open it up in chrome, run an extension, close both tabs, repeat process on next url.
  3. Find links that contain a pdf (opposed to video)
  4. Save pdf links to the text edit file.
  5. Run an automator script to save each web page as web archive in safari.
  6. Rename all files from another text edit file using "A Bettter Finder Renamer" based on creation date.

The problem with this is that the pdf's are saved later in the process and therefore messes up the renaming process as the list doesnt matach with the corresponding files.

So the ultimate question is can I run two different applications inside an applescript dependant on the url's text formatting.

I've not got access to all my workflows and scripts currently but I've attached the main script. (Credit goes to Jacques Rioux in Re: Re: Apple Automator to view a list of URLs from a text file)

In Automator:

1- Get Specified Finder Items -- one text file

2- Add "Run Applescipt script" action

Remove all text in the action

Put this script into the action

on run {input, parameters}

read (item 1 of input)

set ps to paragraphs of the result

set tot to count ps

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:p}

set current tab to r

delay 1

if i = tot then exit repeat

repeat

delay 1

get URL of r

end repeat

end tell

end tell

end try

end if

end repeat

end run


(I've tried adding the above as code but it just keeps converting back so if somebody could also help with that!)


I believe I've altered this to run the application before line 12 via a tell. I hope this is clear enough to resolve some of my issues. If not ask away and thanks in advance!

Posted on Oct 27, 2015 2:13 AM

Reply
10 replies

Oct 27, 2015 12:32 PM in response to rccharles

Okay so I have access to my files now:

Im trying to copy files from my online course to my hard rive for offline viewing.

User uploaded file

As you can see they're both video files and text files. To save the video files I have one automator application which goes inside another workflow and to save the web archive I have another. The Workflow is as follows:

User uploaded file

User uploaded file

Lines 12-14 currently tell the script to run that application.


What I'm ultimately after is being able to make the application used variable dependant on the textedit file.

For example I could set it up as below and need the condition:

if paragraph starts with (video) run "X" application

if paragraph starts with (web) run "Y" application


User uploaded file

Hope this is clearer!

Oct 27, 2015 3:15 PM in response to rccharles

Sorry for sounding like a novicebeing a complete novice but could you show me where to insert that into the main code and where I would add the tell expressions to run the applications for: Video=Application1 Web=Application2. I feel like I need to read up on some beginner stuff but I'd like to get this out the way first and you're being a great help.


And how do I post script! I've really rushed into all of this 😟


on run {input, parameters} read (item 1 of input) set ps to paragraphs of the result set tot to count ps 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:p} set current tab to r delay 1 if i = tot then exit repeat repeat delay 1 get URL of r end repeat end tell end tell end try end if end repeat end run

Oct 27, 2015 7:17 PM in response to Sc0lie

I modified the posted script. Don't know if you will be able to download the video or not. Were you able to get a web page and the video by manual means?


on run {input, parameters}
    log "  --- Starting on " & ((current date) as string) & " --- "
    read (item 1 of input)
    --   set result to "(web)https://www.youtube.com/watch?v=-Ur-D37-juo" & return & "(video)http://download.wavetlan.com/SVV/Media/HTTP/BlackBerry.mov" & return
    set ps to paragraphs of the result
    set tot to count ps
    tell application "Safari"
        reopen
        activate
    end tell
    
    repeat with i from 1 to tot
        set p to item i of ps
        log "p =" & p
        if p is not "" then
            log "p = 1 thru 10  " & ((characters 1 thru 10 of p) as string)
            
            set splitData to textToList(p, ")")
            set extractedURL to item 2 of splitData
            log "extractedURL = " & extractedURL
            if ((characters 1 thru 7 of p) as string) is equal to "(web)ht" then
                log "web found"
                -- Ok, I assume you want to invoke safari when have a web page.
                try
                    tell application "Safari"
                        tell front window
                            set r to make new tab with properties {URL:extractedURL}
                            set current tab to r
                            delay 1
                            -- are we done yet? 
                            -- what is different from the last item and the 
                            -- previous item?
                            -- --- if i = tot then exit repeat
                            -- Run the safari window until the user closes the window 
                            repeat
                                delay 1
                                get URL of r
                            end repeat
                        end tell
                        
                    end tell -- tell safari
                end try
                
            else if ((characters 1 thru 7 of p) as string) is equal to "(video)" then
                log "video found"
                -- you need to find some way to download the video.  
                -- For my testing, I picked a .mov file.
                
                set quotedextractedURL to quoted form of extractedURL
                log "quoted form is " & quotedextractedURL
                try
                    set fromUnix to do shell script "cd ~;pwd;id"
                    log "fromUnix = " & fromUnix
                    -- hackie
                    try
                        set fromUnix to do shell script "cd ~;mkdir courseVideos"
                    end try
                    -- place the file in the user home director.
                    set fromUnix to do shell script "cd ~/courseVideos;curl -O " & quotedextractedURL
                on error errMsg
                    log "curl -O  error..." & errMsg
                end try
                
            else
                log "other"
            end if
            
            
        end if -- not ""
    end repeat
end run

-- textToList was found here:
-- http://macscripter.net/viewtopic.php?id=15423

on textToList(thisText, delim)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    try
        set resultList to every text item of thisText
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultList
end textToList

Oct 28, 2015 1:56 AM in response to rccharles

Thank you. I'll give this a try when I'm home. The video and web page are downloaded via an application made in automator


(from memory) – Video

  1. Opens url in Safari
  2. Goes through the develop menu and opens in chrome
  3. Activates a keyboard shortcut to click the screen in two places to run the extension.
  4. Closes the chrome tab
  5. Closes the safari tab


(from memory) – Webpage

  1. Opens url in safari
  2. File > Save as
  3. Closes safari tab


This then allowed the applescript to continue because the safari tab had been closed.

Oct 29, 2015 2:00 AM in response to rccharles

This worked perfectly thank you. Because both actions start in safari and run an application from there I just modified the script to suit my pre-existing workflow.


Thank you so much.


on run {input, parameters}
      log "  --- Starting on " & ((current date) as string) & " --- "
      read (item 1 of input)
      --   set result to "(web)https://www.youtube.com/watch?v=-Ur-D37-juo" & return & "(video)http://download.wavetlan.com/SVV/Media/HTTP/BlackBerry.mov" & return  
      set ps to paragraphs of the result
      set tot to count ps
      tell application "Safari"
            reopen
            activate
      end tell
      
      repeat with i from 1 to tot
            set p to item i of ps
            log "p =" & p
            if p is not "" then
                  log "p = 1 thru 10  " & ((characters 1 thru 10 of p) as string)
                  
                  set splitData to textToList(p, ")")
                  set extractedURL to item 2 of splitData
                  log "extractedURL = " & extractedURL
                  if ((characters 1 thru 7 of p) as string) is equal to "(web)ht" then
                        log "web found"
                        -- Ok, I assume you want to invoke safari when have a web page.  
                        try
                              tell application “XYZ"
                                    activate
                              end tell
                              tell application "Safari"
                                    tell front window
                                          set r to make new tab with properties {URL:extractedURL}
                                          set current tab to r
                                          delay 1
                                          -- are we done yet?   
                                          -- what is different from the last item and the   
                                          -- previous item?  
                                          -- --- if i = tot then exit repeat  
                                          -- Run the safari window until the user closes the window   
                                          repeat
                                                delay 1
                                                get URL of r
                                          end repeat
                                    end tell
                                    
                              end tell -- tell safari  
                        end try
                        
                  else if ((characters 1 thru 7 of p) as string) is equal to "(video)" then
                        log "video found"
                        -- you need to find some way to download the video.    
                        -- For my testing, I picked a .mov file.  
                        
                        try
                              tell application “ABC"
                                    activate
                              end tell
                              tell application "Safari"
                                    tell front window
                                          set r to make new tab with properties {URL:extractedURL}
                                          set current tab to r
                                          delay 1
                                          -- are we done yet?   
                                          -- what is different from the last item and the   
                                          -- previous item?  
                                          -- --- if i = tot then exit repeat  
                                          -- Run the safari window until the user closes the window   
                                          repeat
                                                delay 1
                                                get URL of r
                                          end repeat
                                    end tell
                                    
                              end tell -- tell safari  
                        end try
                        
                  else
                        log "other"
                  end if
                  
                  
            end if -- not ""  
      end repeat
end run


-- textToList was found here:  
-- http://macscripter.net/viewtopic.php?id=15423  


on textToList(thisText, delim)
      set resultList to {}
      set {tid, my text item delimiters} to {my text item delimiters, delim}
      try
            set resultList to every text item of thisText
            set my text item delimiters to tid
      on error
            set my text item delimiters to tid
      end try
      return resultList
end textToList

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple Script: Opening urls from Plain Text and Running Separate Actions Depending on Formatting

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.