How Do I get the Default App?


(Jim Underwood) #21

In that case I have no reason to not always include it in my standard script setup. It will now be:

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

Any objections?

(Shane Stanley) #22

The only real drawback is that it adds a lot of terminology to code-completion (as in more than doubling the amount). But otherwise it’s probably no worse than including use scripting additions just in case.

(Phil Stokes) #23

I was categorically told (off) by Apple Engineering about this: don’t include any use statements that are not required in the script at hand.

(Shane Stanley) #24

And I was chided for including use scripting additions rather than wrapping use with using terms from :roll_eyes:

In the real world, I reckon there are worse things to worry about — and particularly leaving them out when they should be included.

(Phil Stokes) #25

I just spent the last 20 minutes trying to find the correspondence (to no avail) as I can’t remember the exact reasons why they said this (I remember the tone, though…let’s say the bedside manner leaves a bit to be desired).

As best as my buggy CerebralOS can remember, it was along the lines of not introducing unnecessary dependencies, possible name clashes, unknown bugs. i.e., nothing specific, just general good coding practice.

(Shane Stanley) #26

It’s certainly best practice. But I think some allowance needs to be made for the target user base. For example, NSAttributedString is in Foundation, but it also has methods in AppKit.

I guess the issue here is that someone — and this isn’t the first time or place I’ve seen this — has suggested they can leave use statements out because it worked for them. If the alternative is to err the other way, that seems to me to be likely to be less troublesome in the long run.

Oh, and I was also chided for using PDFKit instead of Quartz. Maybe we can just use Cocoa :wink:

(For lurkers, I’m only joking about using Cocoa.)

(Nigel Garvey) #27

That’s a bit rough! ASLG’s coverage of use scripting additions says: “If a script uses use scripting additions, AppleScript may optimize scripting addition commands, sending them to the current application instead of the current target (it) when it does not change the meaning to do so. […] Without a use scripting additions command, AppleScript must use a less efficient dispatching scheme, so explicitly declaring them is recommended.” — while at the end of using terms from it says: "using terms from scripting additions does not enable optimization of scripting addition commands as use scripting additions does."

(Phil Stokes) #28

Hmm, I don’t want get into an argument about it, but no, that wasn’t the tenor of the reply I got from Apple. It was more the contrary: include use framework statements only when they’re needed; if you don’t need them, you’ve no business putting them in.

This (to my mind) is the same advice as not importing a framework you don’t need into a Cocoa app, or not importing unnecessary modules into a python script.

FWIW, I think it’s good advice; I just didn’t happen to like the way it was delivered.

(Shane Stanley) #29

But it’s not as bad as it sounds, because the optimisation is only required in code targeting an application, and such code won’t generally compile inside a using terms from scripting additions block anyway.

(Shane Stanley) #30

One of the differences, though, is framework stuff is cached. If you launch with the environment variable ASDebugLibraryLoads set, you’ll see when you run a script with a use framework statement, it’s loaded the first time, and from then on it’s only referenced (speaking on a per-app basis).

I agree with you in principle. I just think it’s a minor issue in the scheme of things.

(Jim Underwood) #31

Thanks to all for this excellent discussion about use statements.

To Shane’s point, I think it makes a difference whether you are writing code for a client to use in a production environment, or in writing code for yourself (and maybe a few friends/colleagues) to use in your/their personal Mac.

It also depends on the type of scripts you will be writing. I’m at the point where many, if not most, of my scripts will directly, or via handlers/script libraries, use ASObjC.

So, since Shane has told us:

I feel comfortable in using a standard set of use statements, not knowing for sure if I will need all of them or not. So, I think I will stick with my default setup:

use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"  -- this may not be required
use framework "AppKit"  -- this may be not be required
use scripting additions

## Some Scripts may work with Yosemite, but no guarantees ##
#  This script has been tested ONLY in macOS 10.11.6+

If at some point in the future I find that including one of these that isn’t required causes a problem, then I will adjust accordingly.

Clash with Foundation & display dialog?
(Shane Stanley) #32

FWIW, you will always need Foundation.

(Jim Underwood) #33

Even if I am not using any ASObjC?

(Shane Stanley) #34

Sorry for the confusion — only when using ASObjC.