Non-Preferred Browser Opening On One Mac But Not Another

Hi,

I am using a Keyboard Maestro macro to launch an AppleScript, to save a .webloc file to my desktop from my preferred browser, Safari. The AppleScript was provided on this Apple Forum page [How to create .webloc files without mousi… - Apple Community] and is pasted below.

It works, as expected, on 2 of my 3 Macs, but on one Mac, it doesn't. Instead it opens Firefox and does not create the .webloc from it.

If I run the AppleScript, independently of Keyboard Maestro, in ScriptEditor it does save the .webloc file from Safari, correctly. So it looks like something weird is happening in Keyboard Maestro... but only on one machine. :thinking:

All 3 of my Macs have Safari as the preferred browser.

Keyboard Maestro sync across the 3 Macs so it should either work or fail on all 3.

Any ideas on what is causing this?

TIA :slight_smile:

--------------------------------------------------------
# Auth: Jasper Robinson @TomasWolpertinger on the Keyboard Maestro Forum
# dCre: 2022/09/26 04:07
# dMod: 2022/09/26 04:07
# Appl: Finder, Firefox, Safari, System Events
# Task: Write a .webloc (Bookmark) File to the Finder from Available Web Browsers.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Firefox, @Safari, @System_Events, @WebLoc, @Bookmark
--------------------------------------------------------
use framework "Foundation"
use framework "AppKit"
use scripting additions
--------------------------------------------------------
property NSMutableDictionary : a reference to current application's NSMutableDictionary
property |NSURL| : a reference to current application's |NSURL|
property NSWorkspace : a reference to current application's NSWorkspace
property NSString : a reference to current application's NSString
property chromium_derivatives : {"Google Chrome", "Chromium", "Opera", "Vivaldi", "Brave Browser", "Microsoft Edge"}
--------------------------------------------------------

set browser_name to my default_browser()
if browser_name is missing value then return

if browser_name contains "Safari" then
   tell application "Safari"
      activate
      tell front document
         set webloc_name to its name
         set webloc_URL to its URL
      end tell
   end tell
else if browser_name contains "Firefox" then
   # until Mozilla provides AppleScript properties
   tell application "Firefox"
      activate
      set webloc_name to name of front window
      tell application "System Events"
         keystroke "l" using {command down}
         keystroke "c" using {command down}
         key code 36
      end tell
   end tell
   tell application "Finder" to set webloc_URL to (the clipboard)
else if chromium_derivatives contains browser_name then
   display alert "Chromium-based browser: " & browser_name & " unsupported at this time." giving up after 10
   return
else
   display alert "Unsupported browser : " & my browser_namer as text & " is unsupported at this time." giving up after 10
end if

set status to my gen_webloc("~/Desktop", webloc_URL, webloc_name)
if not status then display alert "webloc not written to Desktop"

--------------------------------------------------------
on gen_webloc(awhere, aurl, aname)
   # generate a .webloc with default browser's front windows URL and tab name for filename
   set fixed_name to (NSString's stringWithString:aname)'s mutableCopy()
   
   # Just say no to long-winded white space names.
   # fixed_name's stringByReplacingOccurrencesOfString:" " withString:"_"
   fixed_name's stringByReplacingOccurrencesOfString:"." withString:" "
   fixed_name's appendString:".webloc"
   set webloc to ((NSString's stringWithString:awhere)'s stringByAppendingPathComponent:fixed_name)'s stringByStandardizingPath()
   
   # next two lines build .webloc plist skeleton with URL key
   set dict to NSMutableDictionary's dictionaryWithCapacity:1
   dict's setObject:aurl forKey:"URL"
   
   set wstatus to dict's writeToFile:webloc atomically:0
   return wstatus as boolean
end gen_webloc
--------------------------------------------------------
on default_browser()
   # return default browser name that would open this URL
   set URL_Apple to |NSURL|'s URLWithString:"https://www.apple.com"
   set browser_name to ((NSWorkspace's sharedWorkspace)'s URLForApplicationToOpenURL:URL_Apple)'s lastPathComponent()
   return (browser_name's stringByDeletingPathExtension()) as text
end default_browser
--------------------------------------------------------

In which case, showing us just the AppleScript may not be enough. If you could post the actual macro perhaps someone will spot something. And including your OS and Firefox versions would be a great help when trying to replicate the problem.

Thanks, yes.

The Macro just runs the script (Execute AppleScript/Execute test script/ignore results)—there are no other actions.

I've just run it again, now, and it worked. This is the first time it's succeeded on this Mac for over a month. I am even more puzzled now.

I'm on macOS 12.5.1 (21G83) and Firefox is 104.0.2 (64-bit)

3 Webloc file from Safari or Finder.kmmacros (5.7 KB)

Hmm...

Taking a shot in the dark – I wonder if the LaunchServices database was out of wack.

Should this issue occur again I'd suggest rebuilding the LSD with Onyx or an equally appropriate tool.

Then reboot of course.