Page 1 of 1

Copying data from livecode to applescript variable

Posted: Sat Sep 07, 2013 4:33 pm
by ekami
Hello from France
I have a field ( field id 1107 ) containing a string, the path name of a file : "/Users/Moi/My document.txt"
I have a second field containing the native AppleScript (launched with de "do" command), but the first line doesn't works, i can't copy the field to an AppleScript variable
My question is : How can i copy this field in an AppleScript variable ? ( i do this every day with FileMaker, but i don't know how to do this with LiveCode 4.6.4 )
Edit : i have a LiveCode 6.1.0, still not installed : if the 4.6.4 can't do that, maybe the version 6 can ?

Code: Select all

set ABFile to (field id 1107 of this card)
set existe to ""
set theFile to ABFile as POSIX file
tell application "Finder" to if exists theFile then set existe to "yes"
if existe = "yes" then
	set theFile2 to theFile as alias
	tell application "Finder" to set label index of theFile2 to 1
end if

Re: Copying data from livecode to applescript variable

Posted: Sat Sep 07, 2013 11:34 pm
by Simon
Hi ekami,
Welcome to the forum! :)

I'm just a liveCode user so your code looks odd to me, but I think you want to change the color of a files label?
Well if that is true then:

Code: Select all

on mouseUp
   answer file "Which file should I color?"
   put it into theFile
   if there is a file theFile then -- not really necessary because I used the "answer file" command
      replace "/" with ":" in theFile
      delete the first char of theFile
      put "tell application "&quote& "Finder"&quote&" to set label index of alias" && quote & theFile & quote && "to 5"  into tScript
      do tScript as "Applescript"
   end if
end mouseUp
Now I know you used a field, I could have written:

Code: Select all

put field id 1107 into theFile
in place of the first 2 lines (not counting "on mouseUp")

Pity about the "replace" and "delete" lines but it's the way Applescript wants it.

Simon

Re: Copying data from livecode to applescript variable

Posted: Sat Sep 07, 2013 11:36 pm
by Klaus
Bonsoir ekami,

there are several ways to do so :D

I usually put a PLACEHOLDER (including the neccessary QUOTES!) into my AppleScripts like:

Code: Select all

set ABFile to "XXXFieldRefXXX"
set existe to ""
set theFile to ABFile as POSIX file
...
Then I put the AppleScript into a variable and REPLACE my placeholder with the actual value and can then execute the AppleScript:

Code: Select all

...
put fld "applescript" into tAS
replace "XXXFieldRefXXX" with the text of fld "the file path" in tAS
do tAS as "AppleScript"
... 
You get the picture :D


Best

Klaus

Re: Copying data from livecode to applescript variable

Posted: Sun Sep 08, 2013 1:52 am
by ekami
Thanks a lot Simon & Klaus !
I'll finally use the Klaus method, elegant and re-usable in long AppleScripts.
Depending of the variable types to pass to AS, quotes are needed, or not, but the replace method works in both cases.
--
Of course, it would be much more elegant to use a direct statement in AppleScript (as in FileMaker), something as
Copy field "myField" of this card to Var1
or
Copy field id 100 of next card to Var1
or better, sharing global variables beetwen LC & AS :P

Is that planned in future versions of LiveCode ?