Oh well, I have changed login name since I made it originally: Here is a revised version of Pike from post #5
This script relies on auto-swooshing of windows by Spaces/Expose; that is, when you select a window in another space, that space is activated/you are taken to that space (pick your view).
You activate auot-swooshing by entering this from the command line:
-- Pike http://macscripter.net/viewtopic.php?pid=154619#p154619
-- © McUsr and put into public domain you may not post this anywhere else, what so ever!
-- Added a try block, since some applications doesn't allow you to set the visible property
-- of a window.
-- 2013/07/08
-- When saved as an app, and the LSUIElement is not set (Application is an Agent): then
-- The script will err, since there is no frontmost proces. I have "fixed this" by setting "me"
-- as the frontmost process in those cases.
-- Some windows of some apps doesn't even have a visible property.
-- I have also forked out a script object for practical purposes, in the post above.
-- I have removed one more bug, the issue was when there were just one window in this
-- space, and windows in other spaces, and I tried to hide the window, and make it visible again,
-- then I would remain in the other space.
-- Ok, so I removed the choose dialog for the case where the app has just one window totally in every space.
-- There are probably still some glitches here, when an app isn't scriptable and so on, but this is as good as
-- it gets from my side, to pick a window among those you have in the current space, or select from all spaces
-- if you have no more than one window. If you have none, then you command - tab to the app. The script is in
-- post #7 together with a skeleton for an app, so you can use it from various places.
-- Invisible windows, that should have stayed invisible started to turn up as a result of former fixes. It is updated in post #7.
-- Now it moves you into the space where a window resides, if the window you chose didn't reside in the current space,
-- (if you had just one window in this space, you'll get the opportunity to select windows from other spaces, if the app has any).
-- It is all in post #7.
-- Factored out a subroutine, and tested it thoroughly, with document based and non documentbased apps. It should work as intended
-- for applications that are scriptable.
-- One last bug is fixed. The problem was not getting the window to front, when the window is residing in another space.
-- I had to differentiate between cases where there were just one window in another space, and more than one.
-- Perfect, as far as it goes.
-- Apart from when there were multiple windows in the current space. That is fixed now.
# Totally fixed, the right version too..
# fixed bug, when an app that we want to display windows for are somehow busy.
# implemented a workaround for Smile. Thanks to kel!
property tlvl : me
property scripttitle : "Pike"
property parent : AppleScript
property showOthersWhenMoreThanOneInCurrent : true
script windowlib
property curAppName : missing value
property curAppBid : missing value
property curWindow : missing value
to setCallingApp()
set curWindow to missing value
tell application id "com.apple.systemevents"
set {curAppName, curAppBid} to {name, bundle identifier} of (first process whose visible is true and frontmost is true)
if (count every window of application process curAppName) > 0 then
try
set curWindow to name of window 1 of application process curAppName
if curWindow = "" then
set curWindow to missing value
end if
end try
end if
end tell
end setCallingApp
to elevateWindow for windowname by appname
script o
property l : {}
property m : {}
end script
set theWins to missing value
tell application id "com.apple.systemevents"
set itsBid to bundle identifier of process appname
set winCount to count (name of every window of application process appname)
set theWins to name of windows of process appname
end tell
-- we need to know if one of our windows is the first in current space!
set firstWinInThisSpace to firstWindowInCurrentSpace()
log firstWinInThisSpace
-- weird construct if the first window in current space is among the windows in
-- our list, then we know for sure that we are active, AND has the front window here
-- (Important because of Expose's "auto-swoosh" setting that we rely upon).
set inThisSpace to windowname is in theWins
local winpos
if inThisSpace then
set winpos to IndexOfItemNoCase(windowname, theWins)
if winCount > 1 then
selectWindow for windowname by appname from "Current Space"
local m
tell application curAppName
try
set m to count every window
end try
end tell
do shell script "open -b " & itsBid
else # no bother, just one window, maybe it's active..
if firstWinInThisSpace is not in theWins then
tell application appname to activate
end if
end if
else
if firstWinInThisSpace is in theWins then
-- we'll deal with this, by activating another app.
set processToActivate to missing value
tell application id "com.apple.systemevents"
set processToActivate to item 2 of (name of every process whose visible is true)
if processToActivate is not missing value then
set frontmost of process processToActivate to true
# else .. never mind...
end if
end tell
end if
-- We'll also have to figure out if it is first win in another space
tell application appname
set fullWnList to name of windows
end tell
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theWins}
set o's l to text items of (fullWnList as text)
set AppleScript's text item delimiters to tids
set winCount to length of o's l
set i to 1
repeat winCount times
if item i of o's l = "" then
set item i of o's l to missing value
end if
set i to i + 1
end repeat
set o's l to o's l's text
set winpos to IndexOfItemNoCase(windowname, o's l)
# selectWindow for windowname by appname from "Current Space"
selectWindow for windowname by appname from "Other Space"
end if
end elevateWindow
to selectWindow for aName by anApp from aSpace
tell application anApp
tell its window aName
if aSpace is "Other Space" then
activate
end if
try
set index of it to 1
end try
try
set visible of it to true
if anApp is not "Smile" then
set visible of it to false
set visible of it to true
end if
end try
end tell
end tell
end selectWindow
on firstWindowInCurrentSpace()
local winInCurSpace
tell application id "com.apple.systemevents"
set winInCurSpace to (name of every window of (every process whose visible is true and frontmost is true))
local i
set i to 1
repeat (count winInCurSpace) times
if item i of winInCurSpace is not {} then
set winInCurSpace to text of item i of winInCurSpace
exit repeat
end if
set i to i + 1
end repeat
end tell
return winInCurSpace
end firstWindowInCurrentSpace
on IndexOfItemNoCase(theItem, theList) -- credits Emmanuel Levy
set text item delimiters to return
set theList to return & theList & return
set text item delimiters to {""}
try
-1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
on error
0
end try
end IndexOfItemNoCase
end script
on run
script o
property a : missing value
property b : missing value
property f : missing value
end script
local fromApp, d
# we need to set the calling app...
setCallingApp() of tlvl's windowlib
# we'll find every window in the current space.
tell application id "com.apple.systemevents"
set o's a to name of (every process whose visible is true)
repeat with i from 1 to count o's a
# we sift out the crud
if (count every window of (application process (item i of o's a))) = 0 then
set item i of o's a to missing value
end if
end repeat
end tell
set o's a to o's a's text
# time to ask for which app we want to pick a window from...
tell application id "com.apple.systemuiserver"
activate
set fromApp to (choose from list o's a default items item 1 of o's a with title my scripttitle & ": App that has the Window:")
end tell
if fromApp = false then error number -128
# So we find every window of the app in current space.
set fromApp to fromApp as text
tell application id "com.apple.systemevents"
set bid to bundle identifier of application process fromApp
set bn to (name of every process whose bundle identifier is bid) as text
set o's f to name of (every window of application process bn)
end tell
repeat with i from 1 to count o's f
if item i of o's f = missing value then
if fromApp = "Finder" then tell application "Finder" to set item i of o's f to displayed name of target of Finder window i
else if item i of o's f = "" then
set item i of o's f to missing value
end if
end repeat
set o's f to o's f's text
local g
if (count o's f) > 1 and not showOthersWhenMoreThanOneInCurrent then
tell application id "com.apple.systemuiserver"
activate
set g to (choose from list o's f default items item 1 of o's f with title my scripttitle & ": Choose Window:")
end tell
if g = false then
if (tlvl's windowlib's curWindow) = missing value then
tell application id (tlvl's windowlib's curAppBid) to activate
else
elevateWindow of (tlvl's windowlib) for (tlvl's windowlib's curWindow) by (tlvl's windowlib's curAppName)
end if
error number -128
end if
else
try
with timeout of 2 seconds
set o's f to name of every window of application fromApp whose visible = true
end timeout
on error e number n
if n = -1712 then # timeout
tell application id "com.apple.systemuiserver"
activate
try
display dialog (fromApp & " is busy doing something else at the moment.
Please try again later") buttons {"Ok"} with icon 2 with title my scripttitle default button 1
end try
end tell
if (tlvl's windowlib's curWindow) = missing value then
tell application id (tlvl's windowlib's curAppBid) to activate
else
elevateWindow of (tlvl's windowlib) for (tlvl's windowlib's curWindow) by (tlvl's windowlib's curAppName)
end if
error number -128
else
try
set o's f to name of first window of application fromApp
on error
tell application id "com.apple.systemevents"
set o's f to name of (every window of application process fromApp)
end tell
end try
end if
end try
if (count o's f) > 1 then
repeat with i from 1 to count o's f
if item i of o's f = missing value then
if fromApp = "Finder" then tell application "Finder" to set item i of o's f to displayed name of target of Finder window i
else if item i of o's f = "" then
set item i of o's f to missing value
end if
end repeat
set o's f to o's f's text
if (count o's f) > 1 then
tell application id "com.apple.systemuiserver"
activate
set g to (choose from list o's f default items item 1 of o's f with title my scripttitle & ": Choose Window:")
end tell
if g = false then
if (tlvl's windowlib's curWindow) = missing value then
tell application id (tlvl's windowlib's curAppBid) to activate
else
elevateWindow of (tlvl's windowlib) for (tlvl's windowlib's curWindow) by (tlvl's windowlib's curAppName)
end if
error number -128
end if
end if
else
set g to missing value
try
set g to item 1 of o's f
end try
end if
end if
if g is not missing value then
elevateWindow of (tlvl's windowlib) for (g as text) by fromApp
else
# it wasn't a document based app, but something with another kind of window.
tell application fromApp to activate
end if
error number -128
end run
Then you just activate spotlight, type pike, and off you go. (You should set LSUIElement to true.)
But before you do that, be sure to comment out the quit, and drag it into the Dock while it is running. Then you’ll kill it from activity monitor, and then you set LSUIElement to true with Xcode or Property list editor.