Tuesday, September 27, 2022

#1 2022-06-12 04:51:08 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

Determining path to Application without using Path To command

I want determine path to applications without using path to command, because it launches the applications, and I want avoid this behaviour.

I found on the net 1 solution, but it is really slow. And I can't understand why:

Applescript:


posixPathTo("TextEdit.app") of me

on posixPathTo(appName)
   set launchServicesPath to "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
   -- get the path to all executables in the Launch Service Registry that contain that appName
   set appPaths to paragraphs of (do shell script launchServicesPath & " -dump | grep --only-matching \"/.*\\" & appName & "\"")
   return appPaths
end posixPathTo


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#2 2022-06-12 05:09:37 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

Re: Determining path to Application without using Path To command

Update:

I found very faster solution, but maybe someone still has better suggestion:

Applescript:


my posixPathTo:"Image Events"

on posixPathTo:appName
   set bundleID to id of application appName
   tell application "Finder" to return POSIX path of (application file id bundleID as alias)
end posixPathTo:

I was interested as well if this solution will return Posix path for slightly renamed copy of app, but it returns posix path to original application:

Applescript:


my posixPathTo:"Atom copy"

on posixPathTo:appName
   set bundleID to id of application appName
   tell application "Finder" to return POSIX path of (application file id bundleID as alias)
end posixPathTo:

--> /Applications/Atom.app/ <-- SHOULD BE /Applications/Atom copy.app/ instead

Last edited by KniazidisR (2022-06-12 05:37:22 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2022-06-12 05:31:48 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1458

Re: Determining path to Application without using Path To command

KniazidisR. I had both of the following in my notebook. The first is deprecated but works in my testing. Both take a millisecond or less to run.

Applescript:

use framework "Foundation"
use framework "AppKit"

set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set thePath to (theWorkspace's fullPathForApplication:"TextEdit") as text
--> "/System/Applications/TextEdit.app"

--OR--

set bundleID to id of application "System Events"
set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set thePath to ((theWorkspace's URLForApplicationWithBundleIdentifier:bundleID)'s |path|()) as text
--> "/System/Library/CoreServices/System Events.app"

Last edited by peavine (2022-06-12 05:51:10 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2022-06-12 05:53:08 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

Re: Determining path to Application without using Path To command

Update to post #2:

As I checked, the script returns proper Posix path after verifying of slightly renamed application copy.

It seems to me, this is why path to command of Standard Additions firstly opens the application.  That is, to be able to verify it in the case it is not verified by system.

Maybe, I am wrong, and launching the applications is due to the bad path to command design.

Last edited by KniazidisR (2022-06-12 06:16:34 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2022-06-12 06:00:52 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

Re: Determining path to Application without using Path To command

peavine wrote:

KniazidisR. I had both of the following in my notebook. The first is deprecated but works in my testing. Both take a millisecond or less to run.



Thank you, @peavine, for this. Your suggestion is ultra fast and useful. I will use it in the future:

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

my posixPathTo:"Atom copy"
--> "/Applications/Atom copy.app"

on posixPathTo:appName
   set bundleID to id of application appName
   set theWorkspace to current application's NSWorkspace's sharedWorkspace()
   set thePath to ((theWorkspace's URLForApplicationWithBundleIdentifier:bundleID)'s |path|()) as text
end posixPathTo:

Last edited by KniazidisR (2022-06-12 06:08:11 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2022-06-12 07:20:05 pm

robertfern
Member
Registered: 2011-11-29
Posts: 195

Re: Determining path to Application without using Path To command

This previous post has a routine that returns more info

https://macscripter.net/viewtopic.php?id=47903

Offline

 

#7 2022-06-13 01:36:16 pm

robertfern
Member
Registered: 2011-11-29
Posts: 195

Re: Determining path to Application without using Path To command

Here is a more detailed but shortened version of the script in the link above

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

my appInfo:"TextEdit.app"

on appInfo:appName
   local bundleIdentifier, currInfoPlistDictionary, currIsRunning, currProcessID
   --, bundleURL, posixPath, hfsPath, appName, executableName, versionString, isAppleScriptEnabled, allowsUserInteraction, isBackgroundOnly, isRunning, processID, processIDs
   set bundleID to id of application appName
   set theWorkspace to current application's NSWorkspace's sharedWorkspace()
   set currPOSIXPath to ((theWorkspace's URLForApplicationWithBundleIdentifier:bundleID)'s |path|()) as text
   set currInfoPlistDictionary to (current application's NSDictionary's dictionaryWithContentsOfFile:(currPOSIXPath & "/Contents/Info.plist")) as record
   set {currProcessIDs, currProcessID} to {{}, missing value}
   tell ((current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:bundleID) as list)
       repeat with currObj in it
           try
               set end of currProcessIDs to currObj's processIdentifier() as integer
           end try
       end repeat
       if currProcessIDs = {} then
           set currIsRunning to false
       else
           set {currIsRunning, currProcessID} to {true, currProcessIDs's last item} -- Save the newest running instance of the application in a separate return property from the property containing the full list of running application instances
       end if
   end tell
   
   tell currInfoPlistDictionary to return {appName:its CFBundleName, executableName:its CFBundleExecutable, bundleIdentifier:its CFBundleIdentifier, posixPath:currPOSIXPath, hfsPath:currPOSIXPath as POSIX file as text, versionString:its CFBundleVersion, isAppleScriptEnabled:its NSAppleScriptEnabled, isRunning:currIsRunning, processID:currProcessID, processIDs:currProcessIDs}
end appInfo:

Last edited by robertfern (2022-06-13 01:40:49 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)