Tuesday, January 21, 2020

#1 2020-01-08 09:37:31 am

ldicroce
Member
Registered: 2017-11-25
Posts: 165

Presence of 'use framework "Foundation"' alters "get every document"

Why is this working:

Applescript:

use AppleScript version "2.4"
use scripting additions
every document -- OK! It gets every tabs of the open windows of SD

While this one is not working:

Applescript:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
every document -- error "Can’t get every document." -1728

Offline

 

#2 2020-01-08 09:55:53 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3877

Re: Presence of 'use framework "Foundation"' alters "get every document"

Try with telling explicitly to which application are the documents belonging.

Applescript:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theApp to (path to frontmost application as text)
tell application theApp
   every document --> {document "Sans titre" of application "Script Editor"}
end tell

Good point, it works also without 'use framework "xyz"'

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 8 janvier 2020  16:54:02

Offline

 

#3 2020-01-09 12:52:11 am

ldicroce
Member
Registered: 2017-11-25
Posts: 165

Re: Presence of 'use framework "Foundation"' alters "get every document"

Thanks Yvan.
I just wonder why the addition of the use framework "Foundation, triggers the error.
Sometime the login behind AS is confusing ....

Offline

 

#4 2020-01-09 01:16:48 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6143

Re: Presence of 'use framework "Foundation"' alters "get every document"

Without a with framework statement, a call unhandled by the context of a script falls back to the application running the script. With such a statement, such calls are assumed to be ASObjC-related. Otherwise you'd need something like tell framework statements.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2020-01-09 04:31:38 am

ldicroce
Member
Registered: 2017-11-25
Posts: 165

Re: Presence of 'use framework "Foundation"' alters "get every document"

I don't understand. Sorry, it is my lack of knowledge.
But ... this script works fine:

Applescript:

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

set the_path to "Users:ldicroce:Desktop:test.txt"
open for access (file the_path) with write permission
write "I am writing" to file the_path
close access (file the_path) -- a new file is generated.

this one doesn't  (I have tested on 2 different Macs):

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation" -- this is the only difference

set the_path to "Users:ldicroce:Desktop:test.txt"
open for access (file the_path) with write permission
write "I am writing" to file the_path
close access (file the_path)

With the following error: "Can’t make current application into type «class fsrf»."

This means that if I include  the use framework "Foundation", I have to readjust most of my scripts.
I am sure it is not the case, then I am doing something wrong.

Last edited by ldicroce (2020-01-09 05:04:04 am)

Offline

 

#6 2020-01-09 05:08:12 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3877

Re: Presence of 'use framework "Foundation"' alters "get every document"

ldicroce wrote:

I don't understand. Sorry, it is my lack of knowledge.
But this script works fine:

Applescript:

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

set the_path to "Users:ldicroce:Desktop:test.txt"
open for access (file the_path) with write permission
write "I am writing" to file the_path
close access (file the_path) -- a new file is generated.

this one doesn't  (I have tested on 2 different Macs):

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation" -- this is the only difference

set the_path to "Users:ldicroce:Desktop:test.txt"
open for access (file the_path) with write permission
write "I am writing" to file the_path
close access (file the_path)

With the following error: "Can’t make current application into type «class fsrf»."

This means that if I include  the use framework "Foundation", I have to readjust most of my scripts.
I am sure it is not the case, then I am doing something wrong.



In fact all your script use a “not really good” syntax. Here are the correct ones.

Applescript:

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

set the_path to (path to desktop as text) & "test.txt"

set theFileRef to open for access (file the_path) with write permission

write "I am writing" to theFileRef
close access theFileRef -- a new file is generated.

With a small change

Applescript:

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

set the_path to ((path to desktop as text) & "test.txt") as alias

set theFileRef to open for access the_path with write permission

write "I am writing" to theFileRef
close access theFileRef -- a new file is generated.

When we use a framework, several commands no longer apply to alias objects. We must use «class furl» ones.

Applescript:

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

set the_path to ((path to desktop as text) & "test.txt") as «class furl»

set theFileRef to open for access the_path with write permission

write "I am writing" to theFileRef
close access theFileRef -- a new file is generated.

Worse, some applications, Adobe ones, require «class furl» objects even if we don't use frameworks.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 9 janvier 2020  12:08:07

Offline

 

#7 2020-01-09 05:42:59 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6143

Re: Presence of 'use framework "Foundation"' alters "get every document"

ldicroce wrote:

"Can’t make current application into type «class fsrf»."



Yvan's given you workarounds. It's basically a bug in AppleScript when building file (and sometimes alias) specifiers -- you need to use coercions instead.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2020-01-09 09:16:27 am

ldicroce
Member
Registered: 2017-11-25
Posts: 165

Re: Presence of 'use framework "Foundation"' alters "get every document"

I see... I wasn't aware of this bug.
And thank Yvan for the suggestions.
L

Offline

 

#9 2020-01-17 10:32:14 am

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

Re: Presence of 'use framework "Foundation"' alters "get every document"

Yvan Koenig wrote:

When we use a framework, several commands no longer apply to alias objects. We must use «class furl» ones.

An alternative that seems to work in many circumstances where a «class furl» object or AppleScript alias is expected is the file's POSIX path.  Since POSIX paths are easy to work with, I've gotten in the habit of trying that option first to see if it works with a given command. Here is its application to Yvan's example (remember not to include a file keyword before the POSIX path):

Applescript:


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

set the_path to ((path to desktop as text) & "test.txt")'s POSIX path

set theFileRef to open for access the_path with write permission

write "I am writing" to theFileRef
close access theFileRef

Offline

 

#10 2020-01-17 11:04:59 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3877

Re: Presence of 'use framework "Foundation"' alters "get every document"

bmose wrote:
Yvan Koenig wrote:

When we use a framework, several commands no longer apply to alias objects. We must use «class furl» ones.

An alternative that seems to work in many circumstances where a «class furl» object or AppleScript alias is expected is the file's POSIX path.  Since POSIX paths are easy to work with, I've gotten in the habit of trying that option first to see if it works with a given command. Here is its application to Yvan's example (remember not to include a file keyword before the POSIX path):

Applescript:


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

set the_path to ((path to desktop as text) & "test.txt")'s POSIX path

set theFileRef to open for access the_path with write permission

write "I am writing" to theFileRef
close access theFileRef




The problem is that the fact that POSIX paths are accepted by such command has never be officially stated. We may see one day where it fails.

The dictionary of Standard Additions claims :  open for access file : the file or alias

a «class furl» object matches this statement.

Applescript:

set p2d to path to desktop as text

set p2d to p2d as «class furl»
--> file "SSD 1000:Users:**********:Desktop:"

As far as I know, POSIX Path doesn't.
So, as I was thought that “playing with matches” was not good practice, I wouldn't rely upon that.
In fact I flee POSIX path or Finder like the plague.

«class furl» objects are definitely the ones to use.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 17 janvier 2020  18:03:47

Offline

 

#11 2020-01-17 11:37:32 am

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

Re: Presence of 'use framework "Foundation"' alters "get every document"

Yvan Koenig wrote:

In fact I flee POSIX path or Finder like the plague.

I too avoid the Finder whenever possible, because I've gotten spurious results in a number of different circumstances over the years. On the other hand, I don't have an aversion to using POSIX paths based on 10+ years of heavy use without ever encountering a problem (once I've confirmed that a given command accepts POSIX paths, of course.)  But I do agree with you that their use is not documented and could break some day.

Offline

 

#12 2020-01-17 04:35:19 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6143

Re: Presence of 'use framework "Foundation"' alters "get every document"

bmose wrote:

Since POSIX paths are easy to work with, I've gotten in the habit of trying that option first to see if it works with a given command.



With scripting addition commands, that's possibly a reasonable approach. For anything involving an application, however, it's something that caught a lot of people out when sandboxing arrived.

Given the trend in security,  passing file objects -- which arrive at applications with a certain level of implied authorization -- seems to me to a much more future-proof approach, as Yvan says. It's not as if there's any great coding burden involved.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#13 2020-01-17 10:59:57 pm

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

Re: Presence of 'use framework "Foundation"' alters "get every document"

Shane Stanley wrote:

Given the trend in security,  passing file objects -- which arrive at applications with a certain level of implied authorization -- seems to me to a much more future-proof approach, as Yvan says.

Despite using POSIX paths successfully for years, you guys have convinced me to make the switch.  As you say, file objects aren't hard to code.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)