I’m not really a programmer, so sometimes I miss the obvious solution right in front of me. Which is what I am hoping is the case here.
This is what I want: My app has a csv file bundled with it, in which it stores data. Kind of like a log. I want to be able to copy that file from within the app to a folder of choice.
This is what I have:
on exportLog()
set exportLocation to (choose folder with prompt "Select destination for the exported log:") as string
set fileLocation to ((path to current application as string) & "Contents:Resources:")
set fileName to "myLog.csv"
try
tell application Finder
duplicate (fileName of fileLocation) to exportLocation with replacing
end tell
on error
display dialog "Unable to copy the log file."
end try
end exportLog
Now, I’m pretty sure that it’s the duplicate call that’s missing something, but for the life of me, I can’t figure out what. Can anyone point me in the right direction here?
Yes, I think your problem is you need an actual path with the duplicate command. “fileName of fileLocation” is not a path… actually i don’t know what it is. To make that a path you would need to add the two strings together…
set myFilePath to fileLocation & fileName
tell application “Finder” to duplicate file myFilePath to folder exportLocation with replacing
→ notice I placed the word “file” in front of the path because you have to make the string into a file specification that the Finder can understand. Similarly I placed the word “folder” in front of the folder path because that is a string also. The Finder does not understand strings.
Here’s another tip…
You can directly get the path to a file inside your application like this:
set myFilePosixPath to path for resource “myLog” extension “csv”
Notice that the result of the above command is a posix style path (i.e. slash deliminated). You can change that to a mac format file specification that the Finder can understand easily like this:
set myFileSpecification to POSIX file myFilePosixPath
Good luck.
Thank you so much for your tips. I have fiddled some more, using the advice given. I have tried the two variations listed below, and while both build just fine, neither will copy the file. I’ve even double checked that the file is actually in the location to be copied from, just to make sure.
I’ve tried this variation:
on exportLog()
set exportLocation to (choose folder with prompt "Select the destination for your exported log (file will be called myLog.csv):") as string
set myFilePosixPath to path for resource "myLog" extension "csv"
set myFileSpecification to POSIX file myFilePosixPath
try
tell application "Finder" to duplicate file myFileSpecification to folder exportLocation with replacing
on error
display dialog "Unable to copy the log file."
end try
end exportLog
and this:
on exportLog()
set exportLocation to (choose folder with prompt "Select the destination for your exported log (file will be called myLog.csv):") as string
set fileLocation to ((path to current application as string) & "Contents:Resources:")
set fileName to "myLog.csv"
try
set myFilePath to fileLocation & fileName
tell application "Finder" to duplicate file myFilePath to folder exportLocation with replacing
on error
display dialog "Unable to copy the log file."
end try
end exportLog
And still no luck.
Like I said, I’m not really a programmer, so I do apologize if I’m just being stupid and/or slow. ![:slight_smile: :slight_smile:](/images/emoji/apple/slight_smile.png?v=12)
Your first script won’t work because of this line…
tell application “Finder” to duplicate file myFileSpecification to folder exportLocation with replacing
Since myFileSpecification is already a file specification then you don’t need the word “file”, so it should be this…
tell application “Finder” to duplicate myFileSpecification to folder exportLocation with replacing
The second script looks good to me. Try logging the error as follows. Maybe the error will show you what is wrong. If you can’t tell by the error then post it and maybe we can figure it out.
on exportLog()
set exportLocation to (choose folder with prompt "Select the destination for your exported log (file will be called myLog.csv):") as string
set fileLocation to ((path to current application as string) & "Contents:Resources:")
set fileName to "myLog.csv"
try
set myFilePath to fileLocation & fileName
tell application "Finder" to duplicate file myFilePath to folder exportLocation with replacing
on error theError number errorNumber
set errorMessage to "Error Number:" & (errorNumber as text) & space & theError
log errorMessage
end try
end exportLog
That did it!
tell application "Finder" to duplicate myFileSpecification to folder exportLocation with replacing
All it took was for me to remove the extra “file”, like you said. It would have taken sheer luck for me to experiment my way to this, so I really can’t thank you enough. ![:slight_smile: :slight_smile:](/images/emoji/apple/slight_smile.png?v=12)
I’m glad it worked. Now that I looked at the second script again I bet the problem is with this part…
(path to current application as string)
I bet that you get a posix path returned (i.e. slash delimited) from that. You would need to change that to a colon delimited path before using it. In general applescript studio commands return posix paths instead of mac paths.
So just to sum up what you’ve learned, when using file paths they must first be in mac format (i.e. colon delimited), then you use the words file or folder in the commands when they are in string format. If they’re not in string format then those words aren’t needed.
Good luck.