Jump to content

Hiding an application, given path to its executable


Recommended Posts

Hi!

I have a tiny workflow that doesn't work. It's supposed to hide an application, given the path to its executable. Here's a link to the workflow

 

It executes AppleScript, which I just learned for this purpose. It works in Script Editor, but doesn't work when called as an Alfred workflow. Any critiques/ideas?

 

on hideProcess(appPath)
    set allProcesses to missing value
    
    tell application "System Events"
        set allProcesses to every process
    end tell

    repeat with proc in allProcesses
        try
            set procFile to path of file of proc
            set procFilePosixPath to POSIX path of procFile
            if procFilePosixPath contains appPath then
                tell application "System Events"
                    set visible of proc to false
                end tell
                exit repeat
            end if
        end try
    end repeat
    return false
end hideProcess


on run(argv)
    set appPath to item 1 of argv
    hideProcess(appPath)
end run

 

Platform:

 

  • MacOS: 14.7
  • Alfred: 5.5.1

 

Debugger:
 

Hide Current App[Hotkey] Processing complete
Hide Current App[Hotkey] Passing output '' to Automation Task
Hide Current App[Automation Task] Running task 'Identify Front App' with no arguments
Hide Current App[Automation Task] Processing complete
Hide Current App[Automation Task] Passing output '/Applications/Alfred 5.app/Contents/Preferences/Alfred Preferences.app' to Run Script

 

Link to comment

btw, I realize that this is odd:

procFilePosixPath contains appPath

 

I don't understand AppleScript well enough to know why I must use the "contains" predicate rather than "is equal to". Perhaps procFilePosixPath is a list, rather than a string? I just used massive trial & error, because it's not demystified when printing to logfile or dialog box

 

Link to comment

It’s much simpler to do it by first getting the app’s name, then using /usr/bin/osascript (JavaScript):

 

function run(argv) {
  Application("System Events").applicationProcesses.byName(argv[0]).visible = false
}

 

I’ll make a note to add an Automation Task to hide apps at a later release.

Link to comment

Thanks! But unfortunately two applications can have the same process name -- even if they have a different path. For example, if I make two copies of a web browser, or text editor. Even if I rename them to Firefox1.app & Firefox2.app, they'll still both have the process name "firefox"

 

Unless there is a convenient way to change an application's process name (chatgpt's answers don't give me great confidence), this is the only obvious way to satisfy my use-case. (Otherwise, the wrong instance of an app may get hidden)

Edited by sandarius
Link to comment

Yeah, using AppleScript to control apps you can’t really differentiate properly (though since we’re talking about the process here, I’d need to recheck). I remember looking into it and there was even a difference between AppleScript and JXA, where one of them always interacted with the oldest process and the other with the newest. There’s some thread in the forum where I documented those findings but it was easily a decade ago, give or take, so might take a bit to find.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...