Wednesday, November 20, 2019

#1 2019-10-21 02:16:25 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 449

Can't use a variable in a "POSIX file" reference in a "Finder" tell?

Applescript:

set mountPath to "/Volumes/newtees"

tell application "Finder"
   if exists (POSIX file "/Volumes/newtees") then log "yes to 1"
   if exists (POSIX file mountPath) then log "yes to 2"
end tell

set POSIXref to POSIX file mountPath

tell application "Finder"
   if exists POSIXref then log "yes to 3"
end tell

Result:

exists file "newtees" --> true
(*yes to 1*)
exists POSIX file "/Volumes/newtees" --> false
exists file "newtees" --> true
(*yes to 3*)

However, if I move the "set POSIXref to POSIX file mountPath" inside a Finder tell, then it, too fails. With an error: "Finder got an error: Can’t get POSIX file "/Volumes/newtees"."

Please explain, I found it very unintuitive to find that if I type out the text, it works, but if I assign the text to a variable and then use the variable, it doesn't work. But only if done inside a Finder tell. Outside a Finder tell, "set POSIXref to POSIX file mountPath" works fine.

Thanks,

t.spoon.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#2 2019-10-21 03:01:40 pm

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

Re: Can't use a variable in a "POSIX file" reference in a "Finder" tell?

Nigel provides a good explanation of this in:

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

The script that returns false can be fixed by adding "my", which forces the script to evaluate  POSIX file, which the Finder doesn't understand.

Applescript:

set mountPath to "/Volumes/newtees"

tell application "Finder"
   if exists (POSIX file "/Volumes/newtees") then log "yes to 1" --returns true
   if exists my (POSIX file mountPath) then log "yes to 2" --returns true
end tell

Last edited by peavine (2019-10-21 05:57:57 pm)


2018 Mac mini - macOS Mojave

Offline

 

#3 2019-10-21 06:11:34 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 449

Re: Can't use a variable in a "POSIX file" reference in a "Finder" tell?

Thanks! I had tried searching for an answer first, but I didn't find the right terms. Great thread you linked too to explain it, although to me the result is still extremely unintuitive behavior on Applescript's part.


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#4 2019-10-21 06:32:36 pm

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

Re: Can't use a variable in a "POSIX file" reference in a "Finder" tell?

Let me add that the best approach generally is your third one: make file references outside tell blocks. Doing it inside tell blocks is writing code that going to break if the target app ever gets sandboxed.


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

Offline

 

#5 2019-10-21 08:13:06 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 449

Re: Can't use a variable in a "POSIX file" reference in a "Finder" tell?

Yes, I had already modified my code by moving the line that makes the POSIX file outside the "tell" before I even saw the replies, and did read that entire other thread and see that doing so was preferable to using "my."

Elsewhere, I've also used:

Applescript:

set fileExist to (do shell script “[ -f ” & quoted from of filePath & ” ] && echo true || echo false”) as boolean

Which I mainly use when I'm already dealing with paths that start with "~" because I prefer this:

Applescript:


set somePath to "~/downloads/someFile.txt"
set fileExists to (do shell script "[ -f " & somePath & " ] && echo true || echo false") as boolean

to this:

Applescript:


set somePath to "~/downloads/someFile.txt"
set {delimitHolder, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set pathFromHome to text items 2 through end of somePath as text
set AppleScript's text item delimiters to delimitHolder
set theFile to POSIX file ((POSIX path of (path to home folder)) & pathFromHome)
tell application "Finder" to set fileExists to exists theFile

Note to anyone finding this - be careful with the former. While short, and supporting relative paths, it doesn't work with spaces in the file name. I can't just use "quoted form of somePath" because quoted paths don't work with ~ for the user folder. I have a library function for find and replace, so I tend to actually call this with the "somePath" variable being a call to escape the spaces in "somePath."


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#6 2019-10-21 09:31:49 pm

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

Re: Can't use a variable in a "POSIX file" reference in a "Finder" tell?

t.spoon wrote:

Which I mainly use when I'm already dealing with paths that start with "~"



FWIW, System Events understands tilde abbreviation, at least in recent versions of the OS:

Applescript:

set somePath to "~/downloads/shane.zip"
tell application id "com.apple.systemevents" to exists item somePath

But more app dependencies mean more authorization dialogs, so I'd prefer:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

set somePath to "~/downloads/shane.zip"
set somePath to (current application's NSString's stringWithString:somePath)'s stringByExpandingTildeInPath()
current application's NSFileManager's defaultManager()'s fileExistsAtPath:somePath

Which is also about 30x faster than System Events, which is in turn much faster than the shell route. And even if one doesn't know ASObjC, it's relatively readable in terms of what it's doing.


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

Offline

 

#7 2019-10-22 05:50:17 am

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 449

Re: Can't use a variable in a "POSIX file" reference in a "Finder" tell?

Thanks Shane. We have enough people running scripts frequently enough that the time differences here actually do matter. We have scripts that might check the existence of a file 6 times, that are run by 25 people each 40 times a day. So that's ≈ 6,000 runs per day, where even if it's only saving .06 seconds, that's collectively saving people 6 minutes a day.

I just tested these, and on first run, my script run times in Script Debugger were:

ASObjC: 0.56 s
Shell: 0.07 s
System Events: 0.03 s
Finder: 0.01 s

I tried them all again, and found a lot of variation.

Run 2:
ASObjC: 0.00 s
Shell: 0.06 s
System Events: 0.73 s
Finder: 0.00 s

So the time for System Events and ASObjC are all over the place.

I ran System Events 10 more times:
0.04
0.03
0.03
0.02
0.02
0.02
0.02
0.02
0.02
0.02

And ran ASObjC 10 more times and it was 0.00 every time.

Does something have to be initialized with ASObjC? It was only the first run that was many times slower. I tried changing file paths for the test in case it was caching something, but runs with new paths were still 0.00

With System Events it wasn't even the first run that was the slow one, so no idea what's going on there.

I put them each inside a repeat 1000 times loop and got:
ASObjC: 0.19 s
Shell: 49.52 s
System Events: 22.16 s
Finder: 2.53 s

Based on these results, I think I'll just stick with Finder. It was consistently super fast, and I'm familiar with it. Based on the results of the 1000x repeat, ASObjC is way faster - but by "way faster," I mean it's 0.00234 seconds faster. It would collectively save everybody 14 seconds per day, assuming we never get any repeats of my initial .56s run.

As you know from helping me in the past, I'll certainly use ASObjC when it's the only way to get the job done. But 4 people work on this code base, and none of us know ASObjC, so there's a strong preference for avoiding it when it's not necessary until at least a couple of us take the time to learn it. I suppose on the flip side, if we started using it whenever possible that would help us learn it... but that's a scary approach to learning for code that's in production.

Last edited by t.spoon (2019-10-22 05:54:59 am)


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#8 2019-10-22 06:28:16 am

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

Re: Can't use a variable in a "POSIX file" reference in a "Finder" tell?

t.spoon wrote:

I just tested these, and on first run, my script run times in Script Debugger were:



Tests in editors can be wildly inaccurate. They include instrumentation code, and although SD tries to compensate for it, it simply can't be done accurately. Use my Script Geek.app, or use an applet. Editors are especially inaccurate here, where some code sends events and some doesn't. That said, at least some of your results look reasonable -- the differences are big enough.

I tried them all again, and found a lot of variation.



That's partly the nature of AppleScript, but there's also disk access and caching involved here. If you run a repeat loop checking if a file exists, caching means you're mostly timing the overhead of whatever route you chose.

Does something have to be initialized with ASObjC?



It does with everything -- it's just more apparent with ASObjC because it's lazily loaded, only when actually first called. It also become a lot quicker in recent versions of the OS, where I suspect some pre-loading is happening. But yes, it has to load once per AppleScript component instance.

I suppose on the flip side, if we started using it whenever possible that would help us learn it... but that's a scary approach to learning for code that's in production.



It is. But I would have thought the odd two-line snippet like this, even if tucked away in a library, might just be a good place to start.


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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)