Solved

Applescript to perform action

Posted on 2014-02-08
2
360 Views
Last Modified: 2014-02-12
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
Comment
Question by:Geekamo
2 Comments
 
LVL 1

Author Comment

by:Geekamo
ID: 39844010
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
 
LVL 39

Accepted Solution

by:
Eoin OSullivan earned 500 total points
ID: 39845161
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now