Applescript execution error

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

RobertC
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 46
Joined: Sun Feb 04, 2007 3:43 pm

Applescript execution error

Post by RobertC » Fri Aug 04, 2023 7:22 am

Hello,

Getting the creation date of a file with AppleScript produces an execution error when run from the “do” command in LiveCode, but not in the AppleScript editor. Other Finder actions are fine.
I also know there are a few other ways to get the creation date, like the shell Setfile command or by looking in the detailed files. But I want to do it via
do as “applescript”.
This is the code:

Code: Select all

tell application "Finder"
get (creation date of  file (":Users:robertcailliau:Documents:Alphabetical:C:Chronological:1990s:1998:1998-01-20 Salon:Salon.mov" as alias)) as text
end tell
this is the LiveCode for the button:

Code: Select all

on mouseup
    do field 1 as "applescript"
    put the result into field 2
end mouseup
this is the result when run from the AppleScript script editor:
2023-08-04 07-55-33.jpeg
this is in LiveCode:
2023-08-04 07-56-12.jpeg
Again, (1) I know there are other ways but they are not what I want, (2) other Finder actions work OK, so why is it seemingly just ”get creation date”?
The Old Rant Robert.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Applescript execution error

Post by stam » Fri Aug 04, 2023 12:59 pm

Does this need to be AppleScript?

LC offers a way to get this - and would be cross-platform (although creation date is Mac/Win only): The detailed files (search dictionary for 'files')

the files (long form).jpg

HTH
S.

RobertC
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 46
Joined: Sun Feb 04, 2007 3:43 pm

Re: Applescript execution error

Post by RobertC » Fri Aug 04, 2023 2:17 pm

stam,
Please read: “I also know there are a few other ways to get the creation date, like the shell Setfile command or by looking in the detailed files. … … I know there are other ways but they are not what I want,”

The question is: why “creation date” fails when other actions do work.

So: HTH? no, it did not, and is just in the way.
The Old Rant Robert.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Applescript execution error

Post by FourthWorld » Fri Aug 04, 2023 2:54 pm

Good to see you here, Robert.

May I ask why it's important to only use AppleScript for that operation?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Applescript execution error

Post by stam » Fri Aug 04, 2023 4:21 pm

RobertC wrote:
Fri Aug 04, 2023 2:17 pm
So: HTH? no, it did not, and is just in the way.
I enjoy sarcasm as much as the the next bloke... rudeness not so much, especially when the intention is to help.
A better choice of words might go a long way...

Nevertheless, without knowing any appleScript I googled this for 5 mins and for me this works:

The AppleScript:

Code: Select all

set fileToProcess to choose file with prompt "Select a file"
tell application "Finder"
	set theFileDate to (creation date of fileToProcess as string)
end tell
display dialog theFileDate
return theFileDate & " || " & fileToProcess
I stored this in a custom prop "aScript" of a button and the button's mouseUp is:

Code: Select all

on mouseUp
    do the aScript of me as "applescript"
    put the result
end mouseUp
This both prompts for a file, shows the creation date in a dialog and puts both the creation date and the file path in the message box
Last edited by stam on Fri Aug 04, 2023 5:51 pm, edited 1 time in total.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Applescript execution error

Post by stam » Fri Aug 04, 2023 4:49 pm

It seems likely that trying to make it a 1-line script has caused your problems, because splitting it into 2 lines works just fine:

Code: Select all

tell application "Finder"
	set fileToProcess to "Macintosh HD:Users:stam:Documents:mdm.sqlite" as alias
	get (creation date of fileToProcess as text)
end tell
returns the creation date as expected.
Screenshot 2023-08-04 at 16.42.52.png
where the code in the LC button is:

Code: Select all

on mouseUp
    do field 1 as "applescript"
    put the result into field 2
end mouseUp


Just in case it isn't clear, your AppleScript seems malformed with an errant parenthesis; and the keyword 'file' doesn't seem to be needed as you're already defining the path as an alias (although what do I know, I last tinkered with AppleScript 20+ years ago and all of the above has come out of about 5 min with google and 10 min tinkering).

Your syntax is

Code: Select all

get (creation date of file(<appleScriptFilePath> as alias)) as text
Based on the above, the 1-line syntax that worked for me was:

Code: Select all

get (creation date of (<appleScriptFilePath> as alias) as text)
Screenshot 2023-08-04 at 17.00.02.png


Equally important: every time I tried missing the hard drive name as you have (i.e. the path starts with :Users: or |:Users:) it resulted in an execution error in LC... It could just be that the error is in your file path.
Did you by chance just replace the "/" with ":" after getting the file path from LC?
Did you try to use the fully qualified old-style MacOS file path <hard drive>:<folder>:<file>?


In the event you did get the file path from LC and manipulated this to try and generate an AppleScript path, you should probably just do the conversion in AppleScript to avoid silly errors:

To translate a UNIX path (file or directory, valid or not) into an AppleScript file reference with AppleScript, use POSIX file

Code: Select all

set <appleScriptPath> to POSIX file <posixPath> 

RobertC
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 46
Joined: Sun Feb 04, 2007 3:43 pm

Re: Applescript execution error

Post by RobertC » Fri Aug 04, 2023 7:44 pm

stam:

Now THAT was much more helpful and indeed supplies the answer: one has to specify the volume explicitly when in LC.

So thank you for finding that!

Further comments though:

- if there was an errant parenthesis, it was probably a copying error because I carefully tested all cases I showed. If my ageing eyesight is right, it was actually a missing parenthesis at the end, which is consistent with a text selection mistake, sorry.

- in the Applescript editor, not putting the volume does work, so that is really confusing. It is unclear why it has to be specified in LC but not in Script Editor.

- your first answer just looked as if you had not noticed that I went to the trouble to specify twice that I knew about other ways and even mentioned “detailed files”. Most often when I post any question on any forum, the first answer that comes back is a variation on “why do you want to do that?” or often something that I explicitly said I did not want.

- I had also searched the web, but found nothing related to why the statement works in the Editor but not in LC. I did not come across anything specifying that the startup volume name must be given. Did you?

- However, as you spotted, the absence of the startup volume name is apparently what the problem was. So, thanks again for that.

Have a nice WE,
The Old Rant Robert.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Applescript execution error

Post by stam » Fri Aug 04, 2023 9:59 pm

No, I found nothing relating to the volume name, but it stood out looking at your code, as in a different lifetime I did spend a lot of time with HFS file paths and always had to include the volume name...

The fact that it works in the AppleScript editor probably suggests the editor has been made very flexible/capable rather than any semblance of correctness; as far as I know there is no shorthand to avoid putting the volume name in. As mentioned the safest thing to do if handling POSIX file paths in AppleScript is to convert it with the Posix file command in AppleScript.

I say errant parenthesis because when I googled online, these 2 statements seemed correct:

Code: Select all

set fileToProcess to "Macintosh HD:Users:stam:Documents:mdm.sqlite" as alias
get (creation date of fileToProcess as text)
which if made into one line would be:

Code: Select all

get (creation date of ("Macintosh HD:Users:stam:Documents:mdm.sqlite" as alias) as text)
whereas your code was

Code: Select all

get (creation date of file("Macintosh HD:Users:stam:Documents:mdm.sqlite" as alias)) as text
I honestly can't tell you if one is more correct that the other, but given that the 2 statements above stood on their own when googling stuff I'd lean more to the former being correct.

As to my first answer - yes you are correct, I didn't notice you specified that and only had a few mins free at the time so thought I would help... but I'll admit I didn't enjoy the tone of the response. A polite disambiguation would have sufficed...

RobertC
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 46
Joined: Sun Feb 04, 2007 3:43 pm

Re: Applescript execution error

Post by RobertC » Sat Aug 05, 2023 10:51 am

It's possible that the editor behaves differently to what normal Applescript does.
However, Keyboard Maestro (KM) also runs the script without the volume name.

In all three cases the actual source text is somehow transformed by some engine into interpretable code of some sort.
In a different life, to quote you, I also used to work with deeper-down code (was a NeXT developer for a while, then an Apple developer, but it was many decades ago).
I have no clue at what level the source code is handled. Perhaps LC has an “older” version of that engine, while both the editor and KM use a newer one which inserts the startup volume if no root volume is present.
Yet, that must happen during actual execution, because this works in the editor and in KM:

Code: Select all

tell application "Finder"
set A to ":Users:robertcailliau:Doc"
set B to "uments:Alphabetical:C:Chronological:1990s:19"
set C to "98:1998-01-20 Salon:Salon.mov"
	get (creation date of file ((A&B&C) as alias)) as text
end tell
but fails in LC. (OK, too many parentheses, I just wanted to be very explicit).
From this I hypothesise that the problem happens quite low down, and only Mark Waddingham can tell us.

And this attempt by the Apple people to make Applescript more tolerant has in actual fact wasted a lot of time for both of us. I much prefer the old times when there were very explicit error messages and code was hard-compiled instead of semi-interpreted. I spent far less time chasing bugs. Once I wasted an entire afternoon because I had used JavaScript's “+” for string concatenation in a php program. Php produced strange results which were correct in some cases but failed in others. But never any error message.

As to the tone of my first response: it's best not to answer in haste. When I get similar answers on other forums it was usually an immediate response, by someone who had failed to take the time to read. Thus I am now taking great care to be specific. :-(

But, together we did find the problem, and a work-around, but not a solution, which lies with the LC coding team. I'll file it as a bug.
And hope we can be friends.
The Old Rant Robert.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Applescript execution error

Post by FourthWorld » Sat Aug 05, 2023 11:28 am

RobertC wrote:
Sat Aug 05, 2023 10:51 am
As to the tone of my first response: it's best not to answer in haste. When I get similar answers on other forums it was usually an immediate response, by someone who had failed to take the time to read. Thus I am now taking great care to be specific. :-(
This is why I often ask "why?".

There are so many ways to do so many things, it rarely hurts to learn more about what's needed.

Here, as you noted, AppleScript's design often makes it problematic. Moreover, ever since Steve came back it appears to be maintained only grudgingly, at the demand of an ever smaller prepress segment.

When it works, it works. When it doesn't, it's murky.

FWIW, for getting file info I'd use LC's built-in command for that. For setting creation date I'd shell the Unix touch command.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Applescript execution error

Post by bn » Thu Aug 10, 2023 7:53 am

@Robert, @Eric

I tried Robert's original AppleScript and it works for me from LC. It even works without the hard drive name prepended as long as there is a colon before "users"

I think what happens is a permission problem. You have to grant Livecode the permission the access to inspect your disk.
Permissions on Mac are ever more restrictive and a bit opaque for me.

I am using "Monterey" but used these settings for a longer time now.

In "System Settings" (the second entry from the Apple Menu) in "Data Protection" I granted access permission for LC in

"Files and Folders"
"Hard disc access"
"Accessability"

I do not know if all of those permissions are necessary.

Kind regards
Bernd

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Applescript execution error

Post by stam » Thu Aug 10, 2023 10:56 am

bn wrote:
Thu Aug 10, 2023 7:53 am
@Robert, @Eric
I think what happens is a permission problem. You have to grant Livecode the permission the access to inspect your disk.
Permissions on Mac are ever more restrictive and a bit opaque for me.
Except when I tried the original code in LC I got a script execution error as well (seemed to work well enough in the AppleScript script editor).
Modifying it made it run correctly in LC (no permissions asked for or given, other than what was there already).
I gather the modified code worked for Robert as well, and it didn't seem like he had to grant permission or he would have said so...

S.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Applescript execution error

Post by bn » Thu Aug 10, 2023 11:31 am

stam wrote:
Thu Aug 10, 2023 10:56 am
Except when I tried the original code in LC I got a script execution error as well (seemed to work well enough in the AppleScript script editor).
Modifying it made it run correctly in LC (no permissions asked for or given, other than what was there already).
If I see that correctly your script accesses the file system directly. I think that LC has to get permission to access the file system by default. (how would you save your stacks otherwise?)

However Robert tried to script the "Finder". That might be the difference. (tell application "Finder")
And if you use AppleScript it is probably a good idea to grant permissions because you will soon run into those permission problems since it is not uncommon to script the "Finder".

Kind regards
Bernd

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Applescript execution error

Post by stam » Thu Aug 10, 2023 11:50 am

Hi Bernd, I scripted the Finder as well...

to be clear, my modified script was:

Code: Select all

tell application "Finder"
	set fileToProcess to "Macintosh HD:Users:stam:Documents:mdm.sqlite" as alias
	get (creation date of fileToProcess as text)
end tell
the only 2 differences with the OP code is splitting the command into 2 lines (and when made into 1 line, a closing parenthesis is in a different position) and including the hard drive in the text representation of the HFS file path.

Robert found that adding the hard drive to his code fixed the issue. I couldn't find a clear reference to an AppleScript or other HFS file path about shorthand for omitting the drive name - all references I've seen include this. I suspect the AppleScript script editor and related systems allow some flexibility to omit this but the way LC processes it doesn't because the same code worked for Robert (and me) in the AppleScript script editor but failed in LC...

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Applescript execution error

Post by bn » Thu Aug 10, 2023 11:56 am

stam wrote:
Thu Aug 10, 2023 11:50 am
Hi Bernd, I scripted the Finder as well...

to be clear, my modified script was:

Code: Select all

tell application "Finder"
	set fileToProcess to "Macintosh HD:Users:stam:Documents:mdm.sqlite" as alias
	get (creation date of fileToProcess as text)
end tell
Hi Stam,

Sorry to have looked at a different script and did not notice the one you quoted.
Then I run out of ideas why Robert's original script did not work. I used

Code: Select all

tell application "Finder"
	get (creation date of file (":Users:myUserName:Documents:Revolution test stacks:utilities:RunRevForum:Jean Marc Radigois:TextArrayTest Kopie.livecode" as alias)) as text
end tell
which is pretty much what Robert scripted.

Post Reply

Return to “Mac OS”