Monday, April 22, 2019

#1 2019-02-17 02:25:47 pm

GG
Member
Registered: 2019-01-14
Posts: 98

How to set the default app to the currently executing app?

I'm having a bit of trouble with setting the default application.

In the following extract from my code; it's likely something simple, but I'm not seeing a way to fix it. I should note that theFile is in POSIX format.

Applescript:

               -- Associate MetaProxy with the new metadata file...
               tell application "System Events"
                   set default application of theFile to (path to application (my name))
               end tell

currently generates the error -1700:

Applescript:

"System Events got an error: Can’t make application \"New MetaProxy\" into type constant."

The other question relates to globally associating a certain file extension with a particular app. I'd be interested in hearing about that too. I realize this can be done using the context menu of finder and also from the get info pane. However; I'd like to do this under program control.

Last edited by GG (2019-02-17 02:26:23 pm)

Offline

 

#2 2019-02-17 06:54:10 pm

GG
Member
Registered: 2019-01-14
Posts: 98

Re: How to set the default app to the currently executing app?

I'm getting there...

I finally found out how to get the application path and bundle id whether inside or outside Script Debugger.

The "standard" preamble for accessing the StdUserDefaults is not quite enough.

The following was found on the Script Debugger forum and slightly augmented by myself to make it function in an equivalent way both inside and outside the Script Debugger.

I believe this will now make it possible for me to have the Browser Id available to set the default app for a file of a given extension.

The way to set the default app for all files of a given extension is still something I haven't figured out.

Applescript:

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

on open theFiles
end open

on run
   
   -- global for convenience...
   global theDefaults
   global myPath
   global myID
   
   -- local for safety...
   local id
   local theData
   local infoDict
   
   -- Additional code to address use of Script Debugger or other host app...
   if my id = missing value then error "This code works only in an applet or script bundle."
   if current application's id = my id then -- must be running as applet
       -- Added to make sure same info is available
       -- inside and outside the Script Debugger or other host app
       set myPath to POSIX path of (path to me)
       set myID to (get bundle identifier of (info for myPath))
       -- The following was already there
       set theDefaults to current application's NSUserDefaults's standardUserDefaults()
   else -- being run by Script Debugger or other host app
       -- This was the part that was new to me
       set myPath to POSIX path of (path to me)
       set theData to current application's NSData's dataWithContentsOfFile:(myPath & "Contents/Info.plist")
       set infoDict to current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value)
       set myID to infoDict's objectForKey:"CFBundleIdentifier"
       -- The following was already there
       set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:myID
   end if
   
   -- Added just to show the results...
   set theInfo to ¬
       "My Application Path = " & (myPath as string) & return & return & ¬
       "My Bundle Id = " & (myID as string)
   display dialog theInfo
   
end run


Filed under: CFBundleIdentifier

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)