How Can I Get List of Bundle IDs of Web Browsers Installed?

My end objective is as stated in the Title.
Maybe this can all be done using ASObjC – that would be great.

I have been trying to integrate two scripts:

  1. Swift script that returns list of app paths – written by a colleague.
  2. My AppleScript to get Bundle ID given the app path

I’m doing this using Keyboard Maestro, which I know most of you don’t use.
But taking the output of the Swift script into my AppleScript is working.

I’d like to just run the Swift script from my AppleScript, but it appears it is harder than running a normal shell script.

Here’s what I have:

Part 1 – Run Swift Script in AppleScript

==UPDATED==: 2020-06-26 01:55 GMT-5

  • replace Swift script with correct script
use AppleScript version "2.5"
use scripting additions
use framework "Foundation" -- Required for all ASObjC commands
use framework "AppKit"

set swiftCmd to "#!/usr/bin/swift
import Foundation

let theURLStrg = \"http://\"
let theURL = CFURLCreateWithString(nil, theURLStrg as CFString, nil)

let appIdentifierKey = \"CFBundleIdentifier\" // App ID

guard let allHandlers = LSCopyApplicationURLsForURL(theURL!, .viewer) else {
    fatalError(\"No handlers for '\\(theURLStrg)' found.\")
}

let appURLs = (Unmanaged
    .fromOpaque(allHandlers.toOpaque())
    .takeUnretainedValue() as NSArray)
    .componentsJoined(by: \"\\n\")

print(appURLs)
"

set appListStr to do shell script swiftCmd

That immediately fails with this error:

sh: line 1: import: command not found
sh: line 3: let: =: syntax error: operand expected (error token is "=")
sh: -c: line 4: syntax error near unexpected token `('
sh: -c: line 4: `let theURL = CFURLCreateWithString(nil, theURLStrg as CFString, nil)'

I don’t have a clue about resolving this.
Can anyone help?

I would like to learn two things:

  1. How to do the entire task in ASObjC
  2. How to Run a Swift script in AppleScript

TIA.

In case anyone needs it, here is the Swift script un-escapted:

Swift Script

import Foundation

let theURLStrg = "http://"
let theURL = CFURLCreateWithString(nil, theURLStrg as CFString, nil)

let appIdentifierKey = "CFBundleIdentifier" // App ID

guard let allHandlers = LSCopyApplicationURLsForURL(theURL!, .viewer) else {
    fatalError("No handlers for '\(theURLStrg)' found.")
}

let appURLs = (Unmanaged
    .fromOpaque(allHandlers.toOpaque())
    .takeUnretainedValue() as NSArray)
    .componentsJoined(by: "\n")

print(appURLs)


You can’t do the whole thing in ASObjC, unfortunately.

I’m no expert of Swift, but I believe you will have to save the Swift code to a file, give it execution permission, and run it from there. It’s not madly fast.

But this will do what you describe:

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set swiftCmd to "#!/usr/bin/swift
import Foundation

let theURLStrg = \"http://\"
let theURL = CFURLCreateWithString(nil, theURLStrg as CFString, nil)

guard let allHandlers = LSCopyApplicationURLsForURL(theURL!, .viewer) else {
    fatalError(\"No handlers for '\\(theURLStrg)' found.\")
}

let appURLs = (Unmanaged
    .fromOpaque(allHandlers.toOpaque())
    .takeUnretainedValue() as NSArray)
    .componentsJoined(by: \"\\n\")

print(appURLs)
"
set thePath to POSIX path of ((path to temporary items as text) & "Test.swift")
try
	set fileRef to (open for access thePath with write permission)
on error errMess number errNum
	close access thePath
	error errMess number errNum
end try
set eof fileRef to 0
write swiftCmd to fileRef as «class utf8»
close access fileRef
do shell script "chmod +x " & quoted form of thePath
set appList to paragraphs of (do shell script quoted form of thePath)

set theIDs to {}
repeat with URLString in appList
	set theURL to (current application's NSURL's URLWithString:URLString)
	set theBundle to (current application's NSBundle's bundleWithURL:theURL)
	set end of theIDs to theBundle's bundleIdentifier() as text
end repeat
return theIDs
1 Like

Sorry to hear that.

Thanks, Shane. That worked perfectly and gives me a good model for running other Swift scripts.
I’m not really into Swift, but some others are and there are a few Swift scripts that I need to run.

Mark @alldritt, what happened to the “Solved” checkbox on the posts?
I wanted to check Shane’s post a solving my problem.

I’m not sure. They are present for the topics I’ve created. I see that a few topics have been marked solved over the last week or so.

@JMichaelTX, I’ve checked Solution on your behalf. I’m not sure why it’s not showing at your end (I get it when I click on the ellipsis icon here).

I get heart, flag bookmark and link when I click the ellipsis icon. No solved. If I click it on my own post I also get pencil (edit)

Thanks, Shane. There appears to be something going on behind the scenes here. As of right now, I can see and choose the “Solution” checkbox:

I think it normally appears only for the OP of the thread, and moderators.
Maybe something changed with the Discourse SW and/or with my Browser (Brave Browser).

IAC, it’s working now. :wink: