Page 1 of 1

Problem copying a file

Posted: Fri Dec 10, 2021 9:52 pm
by kaveh1000
Hi all. Here is my script for a button to copy a file into a subdirectory of the original file.

Code: Select all

on mouseup
   answer file "Please select an image file" as sheet
   put it into tFile
   set the itemDelimiter to "/"
   -- grab file name
   put the last item of tFile into tFileName
   put item 1 to -2 of tFile into tCurrentFolder
   put tCurrentFolder & "/" & "temp/" into tDestinationFolder
   revCopyFile tFile, tDestinationFolder
   answer the result
end mouseup
I get a result of execution error. I am on a Mac and looking at previous posts I understand that this is an AppleScript error. Can anyone tell me what I am missing?

Minimal stack attached.

Re: Problem copying a file

Posted: Sat Dec 11, 2021 12:21 am
by Klaus
Hi Kaveh,

I never used revcopyfile, I always let LC do this kind of actions.
Do this:

Code: Select all

on mouseup
   answer file "Please select an image file" as sheet
   put it into tFile
   set the itemDelimiter to "/"
   -- grab file name
   put the last item of tFile into tFileName
   
   ## Now create target filename:
   put tFile into tTargetFile
   
   ## We simply overwrite that pathname:
   put ("temp/" & tFileName) into last item of tTargetFile
   
   ## Now use put url binfile...  to let LC copy that thing:
   put url("binfile:" & tFile) into url("binfile:" & tTargetFile)
   
   ## No uneccessary alerts... ;-)
   if the result <> EMPTY then
      answer ("TOOT TOOT TOOT" & CR & the result)
   end if
end mouseup
Best

Klaus

Re: Problem copying a file

Posted: Sat Dec 11, 2021 9:47 am
by bn
Hi Kaveh,

I tried your script and it did not work for me. Then I was wondering if it is a permission problem. I am using MacOS 10.14.4 Mojave.
I copied the script that LC makes for AppleScript

Code: Select all

tell application "Finder"
	duplicate file "root:Users:user:Desktop:Bildschirmfoto 2021-11-19 um 12.17.12.png" to folder "root:Users:user:Desktop:temp"
	return the result as text
end tell
And tried to run it from "ScriptDebugger" an application for debugging appleScript.
ScriptDebugger put up a dialog "You have to give ScriptDebugger permission to access "Finder" in order to run this script"
After I granted ScriptDebugger permission the script worked and did the copying.

So my conclusion is that LC does not have permission to acccess "Finder" for revcopyfile to work.

Permissions get to be more and more restrictive and confusing.

Kind regards
Bernd

Re: Problem copying a file

Posted: Sat Dec 11, 2021 10:15 am
by bn
@Kaveh,

I played with permissions for LC in "Security" in "System Settings" of the Mac and it turned out that if I grant LC permission to use "Automation" and allow Finder access then your script works fine from the IDE.

Kind regards
Bernd

Re: Problem copying a file

Posted: Sat Dec 11, 2021 2:15 pm
by kaveh1000
Thank you Klaus and Bernd for your suggestions.

Looks like Apple is making it hard for Apps to use the finder...

I added LiveCode to permissions for automation (see shot) but still get an error and no file copied.

Meanwhile I am trying to use Klaus' method but again getting error. Getting the Toot Toot message you put in. I don't know how to debug it. I tried

Code: Select all

put url("binfile:" & tFile) into tURL
put tURL
put tURL into url("binfile:" & tTargetFile)
but of course it is a binary file. Any suggestions?

Re: Problem copying a file

Posted: Sat Dec 11, 2021 2:18 pm
by Klaus
Waht does the dialog tell you except "TOOT TOOT TOOT"? :D

To be sure:
You already HAVE a folder named "temp" in the same folder containing the image file(s)?

Re: Problem copying a file

Posted: Sat Dec 11, 2021 2:19 pm
by bn
Hi Kaveh,

the screenshot does not show the permissions for "Automation"

Kind regards
Bernd

Re: Problem copying a file

Posted: Sat Dec 11, 2021 2:23 pm
by kaveh1000
Sorry. Attached is Automation. I cannot see how I can add more Apps to it. No + sign below.

Re: Problem copying a file

Posted: Sat Dec 11, 2021 2:28 pm
by bn
Attached is Automation. I cannot see how I can add more Apps to it. No + sign below.
Try to give LC permission to access the "hardDisk", then it will show up in "Automation" (I hope)

Kind regards
Bernd

Re: Problem copying a file

Posted: Sat Dec 11, 2021 4:05 pm
by kaveh1000
Added to full disk access but still not appearing in Automation :-(

Re: Problem copying a file

Posted: Sat Dec 11, 2021 4:58 pm
by bn
I don't know why it does not work. You seem to be on a more recent MacOS than I am.

The script I used is

Code: Select all

on mouseup
   answer file "Please select an image file" as sheet
   put it into tFile
   set the itemDelimiter to "/"
   -- grab file name
   put the last item of tFile into tFileName
   put item 1 to -2 of tFile into tCurrentFolder
   put tCurrentFolder & "/" & "temp" into tDestinationFolder
   if there is a folder tDestinationFolder then
      revCopyFile tFile, tDestinationFolder
   else
      create folder tDestinationFolder
      revCopyFile tFile, tDestinationFolder
   end if
   
   answer the result
end mouseup
Kind regards
Bernd

Re: Problem copying a file

Posted: Sat Dec 11, 2021 5:02 pm
by kaveh1000
Thanks Bernd

Just tried that exact script. I get a folder created so that is some progress. But no file, and I get "execution error".

Re: Problem copying a file

Posted: Sat Dec 11, 2021 5:19 pm
by Klaus
AHA!
That explains why my script did not work for you, it tried to access a non-existing folder. 8)
Maybe you could supply a tad more info next time...

This should do the trick:

Code: Select all

on mouseup
   answer file "Please select an image file" as sheet
   put it into tFile
   set the itemDelimiter to "/"
   -- grab file name
   put the last item of tFile into tFileName
   
   ## Now create target filename:
   put tFile into tTargetFile
   
   ## We simply overwrite that pathname:
   ## Fist check for existence of folder TEMP
   put "temp" into last item of tTargetFile
   
   ## Create if neccessary:
   if NOT (there is a folder tTargetFile) then
      create folder tTargetFile
   end if
   
   ## Create the final filename:
   put ("/" & tFileName) AFTER tTargetFile
   
   ## Now use put url binfile...  to let LC copy that thing:
   put url("binfile:" & tFile) into url("binfile:" & tTargetFile)
   
   ## No uneccessary alerts... ;-)
   if the result <> EMPTY then
      answer ("TOOT TOOT TOOT" & CR & the result)
   end if
end mouseup

Re: Problem copying a file

Posted: Sat Dec 11, 2021 5:27 pm
by kaveh1000
It works!!!!!!!!!!!!!

Thank you so much Klaus. I understand now. My assumption was it would make the folder on the fly!

This is great. I will go thro the script carefully and work on it further and come back with any questions.

Thanks again and to Bernd

Enjoy Sunday.

Regards
Kaveh