I have an applescript that is opens a browser window from a flash projector file. My problme is the projector file is set to run full screen and although the script works perfectly and opens the browser window, it is hidden ‘behind’ the projector file. How do I bring it to the front when the applescript launches the browser?
Hmmm… If you know what is the browser you are launching, you can simply use this:
tell application "Finder"
set frontmost of process "Safari" to true
end tell
If you don’t know what is the browser (eg, you use “open location”), you could try guessing what is the related browser and bring it to the front. Seems that the following code should work for all OS X:
property relatedURL : "http://bbs.applescript.net/"
set theDefaultBrowser to my getDefaultBrowser()
tell application theDefaultBrowser
activate
open location relatedURL
end tell
to getDefaultBrowser()
set dfb to read ((path to preferences as text) & "com.apple.LaunchServices.plist" as alias)
set x to offset of "<key>U:http</key>" in dfb
set dfb to text x thru (x + 800) of dfb
set x to offset of "<key>LSBundleSignature</key>" & (ASCII character 10) & tab & tab & tab & tab & "<string>" in dfb
set browserSignature to text (x + 41) thru (x + 44) of dfb
tell application "Finder" to name of application file id browserSignature
end getDefaultBrowser
For a Mac OS 9 (and back) system, the process whould be more rough:
--> put here creator codes of all know browsers in OS 9
property browserCodes : {"MSIE", "OPRA", "MOZZ", "iCAB"}
open location "http://bbs.applescript.net/"
delay 3 --> while default browser launches
repeat with i in browserCodes
tell application "Finder" to set frontmost of (first process whose creator type = i) to true
end repeat
Of course, there are hundreds of variations which could be better than this (eg, the user could keep opened several browsers and this code may bring to the front the wrong one, etc.).