This is an AppleScript error I got when trying to execute “open location” statement. It only happened when the browser was not launched before the statement was executed (thus it would be fired up by the statement).
I’ve searched ADC and couldn’t find useful info. Could anyone shed some light on this? Thanks.
This seems “kLSLaunchInProgressErr” (opening an already opening application), a problem related to the launch services. What OS are you running?
According to what you describe, perhaps there is some corrupted data in the “http” helper defined in your com.apple.launchservices.plist file?
As Jon said, could you post the related code?
repeat with theRow in selectedDR
if contents of data cell "report" of theRow is "" then
-- do reporting here
set theSource to my loadEmailFromMail(contents of data cell "mid" of theRow, ¬
contents of data cell "result" of theRow)
if theSource is not "" then
-- save the source to sourceF
set rawF to (open for access file (tmpPath & "jmcRaw.txt") with write permission)
set eof rawF to 0 --empty anything previously in the file
write theSource as string to rawF
close access rawF
set submitFN to "jmcSubmit-" & (fnID as string) & ".html"
-- produce a submission HTML
do shell script "python -OO ~/Library/Scripts/junkMatcher/jmc/jmcReport.py ~/Library/Scripts/junkMatcher/temp/jmcRaw.txt /tmp/" & submitFN
open location "file://localhost/tmp/" & submitFN
set contents of data cell "report" of theRow to "+"
set reportedList to reportedList & (contents of data cell "id" of theRow)
set fnID to (fnID + 1)
end if
end if
end repeat
This code is from my regex-based junk filter project (see here if you want to get the full source). Basically what it does is to walk thru a list of selected rows, get the email source from Mail.app based on the info stored in the rows, call a Python script to produce a submission HTML (report spam to SpamCop.net), and open the file up.
(ok now I remember the code at the website is a bit different from the snippet I post here - I changed to use /tmp/ instead of a temp directory in user’s home folder)
EDIT: the error does not always pop up
So I guess jj is making a point - this seems to happen when I tried to report multiple emails, and before I hit the report button the browser is not running. So a rapid sequence of firing up the browser might cause the error.
Now the question: how do I fix this? Looks like open location is asynchronous…
If you knew the name of the default browser, then you could add this simple bit of code to make sure the browser was running after the first “open location” call:
set default_browser to "Safari"
tell application "System Events"
repeat while (name of processes) does not contain default_browser
delay 1
end repeat
end tell
Putting it all together, here’s a handler that you can call after the “open location” call to ensure the script doesn’t continue unless the default browser is running:
my default_browser_running()
on default_browser_running()
set creatorType to word -1 of (do shell script "defaults read com.apple.LaunchServices | grep -C5 E:html | grep -w LSBundleSignature")
set creatorType to <class ktxt> of (creatorType as record)
tell application "Finder" to set default_browser to name of (application file id creatorType)
if ("." is in default_browser) then set default_browser to (text 1 thru -((offset of "." in ("" & (reverse of (characters of default_browser)))) + 1) of default_browser)
tell application "System Events"
repeat while (name of processes) does not contain default_browser
delay 1
end repeat
end tell
return true
end default_browser_running
Thanks Jon - this seems to be working. I juiced up the code a bit by specifying a max number the repeat loop would run - just to make sure that the script won’t hang forever.
For the part that detects ‘.’ thingy, I guess that’s a fallback strategy right? I found Opera (version 6.03) uses “Opera 6.03” as its name. Not sure if this will cause problems.
Hmm, that may cause problems. The reason I added that was because on my system, the name of the default browser returned “Safari.app” but the browser’s name in the process list as returned by System Events is “Safari” (no extension). I added that line, then, to strip the extension if there is one. If the name of the browser contains a version number with a “.” then there could be problems. Good idea about the max loop:
if my default_browser_running() = false then
set the_button to (display dialog "I can't determine if your default browser is launched. If it is, press \"Continue\", if not, press \"Stop\", launch your browser, then run this script again." buttons {"Stop", "Continue"} default button 2)
if the_button = "Stop" then quit
end if
on default_browser_running()
set creatorType to word -1 of (do shell script "defaults read com.apple.LaunchServices | grep -C5 E:html | grep -w LSBundleSignature")
set creatorType to ?class ktxt? of (creatorType as record)
tell application "Finder" to set default_browser to name of (application file id creatorType)
--the next line tries to get the browser name without an extension but may be problematic on file names with version numbers that conatin "."
--if ("." is in default_browser) then set default_browser to (text 1 thru -((offset of "." in ("" & (reverse of (characters of default_browser)))) + 1) of default_browser)
tell application "System Events"
repeat 60 times
if (name of processes) does not contain default_browser then
delay 1
else
return true
end if
end repeat
end tell
return false
end default_browser_running
Jon - in this thread Rob gave a code snippet, which I cite below:
set def_browser to displayed name of (info for alias GetDefaultBrowser())
on GetDefaultBrowser()
set creatorType to word -1 of (do shell script ¬
"defaults read com.apple.LaunchServices |grep -C5 E:html | grep -w LSBundleSignature")
set {text:creatorType} to (text of creatorType) as text
tell application "Finder"
set {defBrowserName, browserContainerAlias} to ¬
{name, container} of application file id creatorType
end tell
return (browserContainerAlias as text) & defBrowserName
end GetDefaultBrowser
Using this doesn’t give me “.app” suffix. I guess the difference is at the line “name if info for alias …”?
No, Rob’s code and mine are essentially the same. The difference is the name of the default browser. If you get info on Safari (command-i), you’ll see that the Name & Extension are “Safari.app” which both Rob’s & my code will return (IE will also show “Internet Explorer.app” and Opera shows “Opera.app”). Not all applications have the “.app” extension and for most, if they do, it is hidden from the user in the Finder but will be returned with the AS code we provided. The real problem is that the internal process name that System Events returns is not necessarily the name that is displayed in the Finder, with or without the extension:
set creatorType to "OPRA" --Opera
tell application "Finder" to set default_browser to name of (application file id creatorType)
-->"Opera.app"
tell application "System Events"
return {(name of processes) contains default_browser, (name of processes)}
-->{false, {"loginwindow", "Dock", "SystemUIServer", "Safari", "Script Editor", "Convert Script to Markup Code", "System Events", "ClassicAuxInput", "Classic Support", "Finder", "UniversalAccess", "Opera 6.03"}}
end tell
As such, this becomes much more difficult to test for the existence of a running process since its internal name (as given by System Events) may be difficult to determine.
Incidentally, while Opera is fast, I get terrible compatibility problems with just about every page I load. I’m sure this is actually a problem with how the webpages are coded and not the browser but Safari and IE are much more forgiving. I’ll sacrifice a bit of speed and go for compatibility any day which is why I stick with Safari.
Hm… running Rob’s code gave me my browser’s name without “.app”. I’ve tried Camino, IE, Safari, Opera (all the browsers I have basically), and none of them gives me names with “.app”.
set creatorTypes to {"sfri", "OPRA", "MSIE"}
set all_names to {}
repeat with creatorType in creatorTypes
set creatorType to contents of creatorType
--my code:
tell application "Finder" to set default_browser to name of (application file id creatorType)
--Rob's code:
tell application "Finder"
set {defBrowserName, browserContainerAlias} to {name, container} of application file id creatorType
end tell
set end of all_names to {my_code:default_browser, Robs_code:defBrowserName}
end repeat
return all_names
-->{{my_code:"Safari.app", Robs_code:"Safari.app"}, {my_code:"Opera.app", Robs_code:"Opera.app"}, {my_code:"Internet Explorer.app", Robs_code:"Internet Explorer.app"}}
Sorry, I didn’t get the first part outside of the subhandler. You’re right, Rob then does account for the discrepancy between the actual name and the displayed name (although it is possible to set the Finder to show the full name with extension as the displayed name using the Finder prefs in which case the two would be the same). A simple modification to either code works:
set creatorTypes to {"sfri", "OPRA", "MSIE"}
set all_names to {}
repeat with creatorType in creatorTypes
set creatorType to contents of creatorType
--my code:
tell application "Finder" to set default_browser to displayed name of (application file id creatorType)
--Rob's code:
tell application "Finder"
set {defBrowserName, browserContainerAlias} to {displayed name, container} of application file id creatorType
end tell
set end of all_names to {my_code:default_browser, Robs_code:defBrowserName}
end repeat
return all_names
-->{{my_code:"Safari", Robs_code:"Safari"}, {my_code:"Opera", Robs_code:"Opera"}, {my_code:"Internet Explorer", Robs_code:"Internet Explorer"}}
This still doesn’t address the issue that the internal name as given by System Events is not necessarily the displayed name either (“Opera” is not equal to “Opera 6.0.3”).