• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 460
  • Last Modified:

Applescript to perform action

Hello Experts,

I am new to Applescript, and to be honest - i don't know anything about it, other than it's code that automates actions.  I know where to paste the code into, but that's about it.

I would like a code, that performs the following steps.

1.)  Script looks at ALL open windows.
2.)  Script brings all windows to the front, that have a specific word in the title.

Btw, the windows I mainly want it looking at are safari windows.

I'm hoping that simple enough to code.
0
Geekamo
Asked:
Geekamo
1 Solution
 
GeekamoAuthor Commented:
And to add...

I could have maybe 15 safari windows open, some may just be popups - some are the main window with tabs.

So I only want this to prefer the action on safari windows that match a specific title.

I am *NOT* looking for 'safari' to be brought to the front, only the safari windows that match.

I hope that made sense.

Thank you in advance for your help.
0
 
Eoin OSullivanConsultantCommented:
This AppleScript finds matching Safari tabs and windows and if only one is found brings it to the front ... if more than one match is found it presents a dialog for you to select
set question to display dialog ("Find Safari tab:") default answer ""
set searchpat to text returned of question

tell application "Safari"
	set winlist to every window
	set winmatchlist to {}
	set tabmatchlist to {}
	set tabnamematchlist to {}
	repeat with win in winlist
		set ok to true
		try
			set tablist to every tab of win
		on error errmsg
			--display dialog name of win as string
			set ok to false
		end try
		if ok then
			repeat with t in tablist
				if searchpat is in (name of t as string) then
					set end of winmatchlist to win
					set end of tabmatchlist to t
					set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
					--display dialog name of t as string
				else if searchpat is in (URL of t as string) then
					set end of winmatchlist to win
					set end of tabmatchlist to t
					set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
					--display dialog name of t as string
				end if
			end repeat
		end if
	end repeat
	if (count of tabmatchlist) = 1 then
		--display dialog "one!"
		set w to item 1 of winmatchlist
		set t to item 1 of tabmatchlist
		set current tab of w to t
		set index of w to 1
	else if (count of tabmatchlist) = 0 then
		display dialog "No matches"
	else
		set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"
		set AppleScript's text item delimiters to "."
		if whichtab is not equal to false then
			set tmp to text items of (whichtab as string)
			set w to (item 1 of tmp) as integer
			set t to (item 2 of tmp) as integer
			set current tab of window id w to tab t of window id w
			set index of window id w to 1
		end if
	end if
end tell

Open in new window


copied from this page
http://hea-www.harvard.edu/~fine/OSX/safari-tabs.html

Unfortunately you cannot cycle through ALL application windows in this manner

There are scripts to get a list of open windows in all applications
tell application "System Events"
   set allWindows to name of window of processes whose visible is true
end tell
set windowList to {}
repeat with i in allWindows
   repeat with j in i
       if contents of j is not missing value then set end of windowList to contents of j
   end repeat
end repeat

Open in new window


You'll have to do some work to create a combination of the two scripts as I'm not in a position to write it for you.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now