Thursday, February 25, 2021

#1 2021-01-22 02:34:34 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 353

Why "a reference to" current application?

When assigning the current application to a property for coding efficiency purposes, I've always used the following form (I happen to use || as the property name, but any variable name will do, of course):

Applescript:


property || : current application

I notice that many users use:

Applescript:


property || : a reference to current application

Is there an advantage to adding a reference to as opposed to leaving it out?

Offline

 

#2 2021-01-22 06:15:09 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Why "a reference to" current application?

Applescript:

property || : 5
set y to a reference to ||
set contents of y to 10
return ||

Last edited by Fredrik71 (2021-01-22 06:18:39 pm)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#3 2021-01-22 10:37:38 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 353

Re: Why "a reference to" current application?

I understand how a variable's value can be changed by making a reference to the variable and changing the contents of the reference.  However, when the reference points to the current application, I can't see a reason why one would want to change the reference's contents to a different value; otherwise, the reference would no longer be pointing to the current application. (Apologies if I am not explaining myself better.)

Last edited by bmose (2021-01-22 10:38:04 pm)

Offline

 

#4 2021-01-23 05:22:32 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 625

Re: Why "a reference to" current application?

If a reference to is a reference type I believe its store its value differently in memory.
But I'm not sure... I find this site if you like to read more.

https://www.tutorialsteacher.com/csharp … rence-type

Don't take my word for it, but its a interesting question

In Objective-C the NSObject is stored as a reference type, so maybe in other words a reference to (shared single copy). But I'm not the expert here... smile

Last edited by Fredrik71 (2021-01-23 05:35:08 am)


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#5 2021-01-23 05:46:31 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5356

Re: Why "a reference to" current application?

bmose wrote:

I notice that many users use:

Applescript:


property || : a reference to current application

Is there an advantage to adding a reference to as opposed to leaving it out?


'current application' and a reference to it are essentially the same thing:

Applescript:

set fred to current application
set bert to a reference to current application
fred is bert --> true

I suspect that people who use 'a reference to current application' do so either to err on the safe side or because they're influenced by the code refactoring offered by the full version of Script Debugger, which can give ridiculous declarations like:

Applescript:

property NSRegularExpressionSearch : a reference to 1024


NG

Offline

 

#6 2021-01-23 08:48:34 am

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 353

Re: Why "a reference to" current application?

Nigel, that's wonderful information. Thank you for the clarification on a point that I had wondered about for a long time.

Offline

 

#7 2021-01-23 09:57:58 am

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

Re: Why "a reference to" current application?

Being new to ASObjC, this is an interesting thread for me. I've read and understand the above posts--the topic of which is making a reference to "current application".

As part of my learning, I was studyiing Shane's PrefsStorageLib.scptd script and noticed the following at the top of the script:

Applescript:

property NSUserDefaults : a reference to current application's NSUserDefaults
property |NSURL| : a reference to current application's |NSURL|

Is there some benefit to using a reference to a class, such as NSUserDefafults?

Using Nigel's test, the situations may not be the same, though. The results on my computer:

Applescript:

use framework "Foundation"
set fred to current application
set bert to a reference to current application
fred is bert --> true

use framework "Foundation"
set fred to current application's NSUserDefaults
set bert to a reference to current application's NSUserDefaults
fred is bert -- false

Last edited by peavine (2021-01-23 10:10:32 am)


2018 Mac mini - macOS Catalina

Offline

 

#8 2021-01-23 11:43:32 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5356

Re: Why "a reference to" current application?

Hi peavine.

The purpose of 'a reference to' is to get an object specifier (eg. 'something of something') rather than the object itself. This can be useful if the specification later applies to another object and you want to use it for that object rather than the original (such as 'window 1 of application "Blah"'), or if the object can't be accessed for some reason at the time you need to set a variable to it. The values of properties are set when a script's compiled, so with these it may be desirable to use 'a reference to' to specify something that'll be on the running machine rather than setting a actual value from the compiling machine.

Values like the enum 'current application' and the integer 1024 are concrete values rather than specifiers, so setting a variable to 'a reference to' one of them is the same as setting the variable to the value directly.

For the most part, a variable set to a specifier can be used exactly like the specifier it contains. But when comparing for equality, the contents of a variable containing a specifier aren't the same as the object itself. So fred isn't equal to bert in the second half of your test script.

Hope this makes sense.  smile


NG

Offline

 

#9 2021-01-23 03:33:21 pm

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

Re: Why "a reference to" current application?

Nigel Garvey wrote:

For the most part, a variable set to a specifier can be used exactly like the specifier it contains. But when comparing for equality, the contents of a variable containing a specifier aren't the same as the object itself. So fred isn't equal to bert in the second half of your test script.

Hope this makes sense.  smile



Nigel. Thanks for the explantion and I think I understand. Would it be somewhat analogous to the reason the first test in the follow repeat loop returns false.

Applescript:

repeat with anItem in {"a"}
   anItem is "a" --> false
   (contents of anItem) is "a" --> true
end repeat


2018 Mac mini - macOS Catalina

Offline

 

#10 2021-01-23 03:40:54 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5356

Re: Why "a reference to" current application?

peavine wrote:

Would it be somewhat analogous to the reason the first test in the follow repeat loop returns false.

Applescript:

repeat with anItem in {"a"}
   anItem is "a" --> false
   (contents of anItem) is "a" --> true
end repeat


Yep. That's exactly right.  smile


NG

Offline

 

#11 2021-01-25 07:07:14 am

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

Re: Why "a reference to" current application?

peavine wrote:

Using Nigel's test, the situations may not be the same, though. The results on my computer:

Applescript:

use framework "Foundation"
set fred to current application
set bert to a reference to current application
fred is bert --> true

use framework "Foundation"
set fred to current application's NSUserDefaults
set bert to a reference to current application's NSUserDefaults
fred is bert -- false



To be the results same, need additional coercion

Applescript:


use framework "Foundation"
use framework "Foundation"

set fred to current application's NSUserDefaults
set bert to (a reference to current application's NSUserDefaults)'s class

fred is bert -- true

So, in the Peavine's example bert is object, but fred is class of this object
Current application is also an object, but as you can see, there is no such confusion with it.
Apparently, AppeScript is smart enough in this case to figure out that you need an object, not its class.

Last edited by KniazidisR (2021-01-25 07:59:03 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#12 2021-01-25 08:31:17 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5356

Re: Why "a reference to" current application?

KniazidisR wrote:

To be the results same, need additional coercion

Applescript:


use framework "Foundation"
use framework "Foundation"

set fred to current application's NSUserDefaults
set bert to (a reference to current application's NSUserDefaults)'s class

fred is bert -- true

So, in the Peavine's example bert is object, but fred is class of this object


The NSUserDefaults class is the object here. fred is the class and bert is a specifier that can be used to obtain that class. It gets a bit confusing when mixing AppleScript with Objective-C as the two types of result aren't shown the same way in editors' Result panes.

The standard way to "dereference" a variable that's been set to 'a reference to' something is with the 'contents' operator, although coercions are sometimes used when the occasion demands.

Applescript:

fred is bert's contents

Normally, of course, you wouldn't use both 'a reference to' and something to dereference it in the same command.  smile


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)