Trouble with safari applescript

Okay I'm relatively new to applescript so pardon if there is a simple solution to this I'm not seeing.


I'm writing a script that asks the user for a string and compares that string to a list of all aggregate urls across multiple safari windows and then opens those corresponding urls in a new safari window.


Here's what I have so far:


--get the URL of every tab of every window

tell application "Safari"

set all_Windows to (get every window) as list

repeat with x from 1 to the count of all_Windows

set this_Window to itemx of all_Windows

set all_Tabs to (get every tab of this_Window) as list

repeat with y from 1 to the count of all_Tabs

set this_Tab to itemy of all_Tabs

set this_URL to (the URL of this_Tab) as string

set URL_Collection to {}

set the end of URL_Collection to this_URL

end repeat

end repeat

end tell


-- User search criteria

set query to text returned of (display dialog "Enter extraction criteria:" default answer "")


--compare to URLs and extract matching links

set matching to {}

repeat with x from 1 to count of URL_Collection

if query is in itemx of URL_Collection then set matching to matching & "," & text itemx of URL_Collection as string


--insert code to close corresponding tab in safari window once URL is saved to matchlist

end repeat


--Open links in new window

tell application "Safari"


makenewdocument


activate

repeat with x from 1 to count of matching


open locationitemx of matching

end repeat

end tell


I'll have to go back through and optimize/error check through the code once I get this functional but I can't even get it to run right now. The section that's giving me problems is the url collection. The script editor event log shows that when I run this right now it seems to collect urls across multiple tabs on two windows successfully. But then it always gets stuck on a 3rd window.



get URL of tab 10 of window id 122897


--> "http://www.acm.uiuc.edu/iCal/workshops/applescript/1999/introduction/conditions. html"


get every tab of window id 122889


--> {tab 1 of window id 122889, tab 2 of window id 122889}


get URL of tab 1 of window id 122889


--> "http://www.quora.com/How-can-I-run-C-code-from-inside-Sublime-Text-2"


get URL of tab 2 of window id 122889


--> "https://sublime.wbond.net/packages/SublimeEvernote"


get every tab of window id 54809


--> error number -1728 from every tab of window id 54809

Result:

error "Safari got an error: Can’t get every tab of window id 54809." number -1728 from every tab of window id 54809


I find it odd that the code segment would work on some windows and not others and I can't for the life of me map it to any logical errors in my code so I'm hoping to gain some insight on that here. Also, and this may or may not be related, but when I ran this script I only had 4 safari windows opened. But the editor event log shows a lot more values being saved for the all_Windows variable.


tell application "Safari"


get every window


> {window id 122897, window id 122889, window id 54809, window id 54808, window id 56238, window id -1, window id 135237, window id 135318}


Any help with this would be appreciated, I'm well and truly stuck. I've googled for hours and I just keep finding variations of the same algorithm I've implemented (nested repeat loops that cycle through windows and tabs) and I've switched through them all but it always get's stuck on that one window and I don't understand how it can work for some windows and not others.

MacBook Air (13-inch Mid 2012), OS X Mavericks (10.9.2)

Posted on May 25, 2014 8:35 PM

Reply
1 reply

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.

Trouble with safari applescript

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