Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 04-17-2008, 04:17 AM   #1
jspitzel
Prospect
 
Join Date: Apr 2008
Posts: 10
How do I "copy' a file in Applescript?

I've do a very little AppleScripting and have an script which copies a file to a burn folder. This worked fine under 10.4 with a Jon's command, but fails under 10.5.
So the question is (and it seems simple) how do I copy a file from one folder to another?
jspitzel is offline   Reply With Quote
Old 04-17-2008, 07:48 AM   #2
appleman_design
Hall of Famer
 
Join Date: Apr 2004
Posts: 2,550
apple-c and apple-v
appleman_design is offline   Reply With Quote
Old 04-17-2008, 08:36 AM   #3
chabig
Hall of Famer
 
Join Date: Jan 2002
Posts: 2,934
Slow down appleman. He wants to script it!

Check out the Finder's dictionary jspitzel. In Script Editor, File -> Open Dictionary. That lists the commands for scriptable apps. In the Finder's dictionary, you'll find:

duplicate v : Duplicate one or more object(s)
duplicate specifier : the object(s) to duplicate
[to location specifier] : the new location for the object(s)
[replacing boolean] : Specifies whether or not to replace items in the destination that have the same name as items being duplicated
[routing suppressed boolean] : Specifies whether or not to autoroute items (default is false). Only applies when copying to the system folder.
→ specifier : to the duplicated object(s)

Chris

Last edited by chabig; 04-17-2008 at 08:41 AM.
chabig is offline   Reply With Quote
Old 04-17-2008, 10:46 AM   #4
raymondlewisjone
Major Leaguer
 
Join Date: Feb 2007
Posts: 411
I, personally, use shell scripts to do it. It is much quicker to use shell scripts than the Finder.

Post your sample script and I will insert and give it back
__________________
Please respond, I am desperate for human contact!
raymondlewisjone is offline   Reply With Quote
Old 04-17-2008, 11:01 AM   #5
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Quote:
Originally Posted by appleman_design
apple-c and apple-v

How did you think this was useful? Do you just rush to be the first reply in threads, regardless of what the person needs or wants?
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"
Mikey-San is offline   Reply With Quote
Old 04-17-2008, 11:02 AM   #6
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Quote:
Originally Posted by jspitzel
I've do a very little AppleScripting and have an script which copies a file to a burn folder. This worked fine under 10.4 with a Jon's command, but fails under 10.5.
So the question is (and it seems simple) how do I copy a file from one folder to another?

Without seeing your code, it's going to be hard for anyone to tell you why it's not working.
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"
Mikey-San is offline   Reply With Quote
Old 04-18-2008, 02:56 AM   #7
jspitzel
Prospect
 
Join Date: Apr 2008
Posts: 10
Smile Re: Copy file in Applescript

Yes - I think I'll shell script too. In fact the applescript runs a Shell Script already so I can just continue that action and bring the Applescript in to do the final burning of the CD (its a backup script).
All help appreciated.
I'll probably be back shortly......
jspitzel is offline   Reply With Quote
Old 04-18-2008, 04:58 AM   #8
jspitzel
Prospect
 
Join Date: Apr 2008
Posts: 10
OK I'm back!
The shell script seemed like such a good idea. As is often the case this turns out to be (or appears to be), a might harder.
OK the problem is I want to copy a file to 'Untitled CD.fpbf' a standard burn folder (empty disk)
The problem is doing a 'do script' with a space in the filename.
I've looked up the Apple blurb and came up with this: (not really sure what it says!)

Quote:
Q: POSIX path doesn’t work right if the file has certain characters in its name — spaces, parentheses, $, *, etc.
A: This is a special case of quoting: you must quote the path to make the shell interpret all the punctuation literally. To do this, use the quoted form of the path. For example, this will work with any file, no matter what its name is:

choose file
do shell script "ls -l " & quoted form of the POSIX path of the result
-- result: "-rw-r--r-- 1 me unknown 1 Oct 25 17:48 Look! a file!"

My do shell script has to look something like this:

do shell script "cp -p "&"/Users/JS/Documents/thefile.gz"&" "&"/Users/JS/Desktop/Untitled CD.fpbf/thefile.gz"

which doesn't work 'cos the 'Untitled CD' needs to be in quotes because of the space.
So how do I put quotes in the do shell script?

help appreciated

Last edited by jspitzel; 04-18-2008 at 08:13 AM.
jspitzel is offline   Reply With Quote
Old 04-18-2008, 09:33 AM   #9
Mikey-San
Hall of Famer
 
Join Date: Jan 2002
Posts: 3,541
Your specific question is answered near the middle/end of this document, but it's worth reading it top to bottom before going further:

http://devworld.apple.com/technotes/tn2002/tn2065.html
__________________
COMPUTER TYPE
SOME SPECIFICATIONS I COPIED FROM THE BOX
STUFF I INSTALLED ALL BY MYSELF
"WITTY QUOTE"

Last edited by Mikey-San; 04-18-2008 at 09:36 AM.
Mikey-San is offline   Reply With Quote
Old 04-21-2008, 05:09 AM   #10
jspitzel
Prospect
 
Join Date: Apr 2008
Posts: 10
Posix paths?

Call me stupid by all means - but I just don't see it.
In the example I gave (which you will note is from the Tech note quoted by you), the POSIX path is exactly what?

I tried to set a variable to the POSIX path of the file for the source and destination.
That seems to make sense, but then how do I do a shell script using variables?
I tried:

set S1 to POSIX path of file "Macintosh HD:Users:JS:Folder:File.gz"
set D1 to POSIX path of file "Macintosh HD:Users:JS:Desktop:Untitled CD.fpbf"
do shell script "cp -p " & S1 & D1

on running this it says Untitled is not a file! or if I put a colon after the .fpbf in the setting of the variable it says Directory CD.fpbf does not exist.
This doesn't seem to make sense - it deals with the space in 'Macintosh HD' but not 'Untitled CD.fpbf'

So I read the TN again and tried the 'quoted path' as in :

set S1 to quoted form of the POSIX path of file "Macintosh HD:Users:JS:Folder:File.gz"

The result is a dialog saying:
Can't get the file "Macintosh HD:Users:JS:Folder:File.gz"
Interstingly, if I don't bother with the variable and do:

do shell script "cp -p " & quoted form of the POSIX path of "Macintosh HD:Users:JS:Folder:File.gz" & quoted form of the POSIX path of "Macintosh HD:Users:JS:Desktop:Untitled CD.fpbf"

I get an error 'Can't make quoted form of POSIX path of "Macintosh HD:Users:JS:Folder:File.gz" of <<script>> into type Unicode text.

That seems contradictory to the TN?
duh - that is the correct path!
I'm going round in circles.
Can you give any more specific help or pointers?

Last edited by jspitzel; 04-21-2008 at 08:50 AM.
jspitzel is offline   Reply With Quote
Old 04-21-2008, 08:48 AM   #11
jspitzel
Prospect
 
Join Date: Apr 2008
Posts: 10
finally!

I've been working at this for hours - but finally it works!!

solution (with help from http://bbs.macscripter.net/viewtopic.php?pid=85208 )

It's quite easy but only if you know the archane syntax rules
Use this to copy a file with a space in the filename using applescript

set Sourcefile to POSIX path of "Macintosh HD:Users:JS:Folder:opera.gz"
set Destination to POSIX path of "Macintosh HD:Users:JS:Desktop:Untitled CD.fpbf:"
do shell script "cp -p " & quoted form of Sourcefile & " " & quoted form of Destination

what a bloody palaver
Thanks to all
jspitzel is offline   Reply With Quote
Old 04-21-2008, 10:08 AM   #12
raymondlewisjone
Major Leaguer
 
Join Date: Feb 2007
Posts: 411
use single quotes or escape double qoutes

do shell script "cp -p "&"/Users/JS/Documents/thefile.gz"&" "&"/Users/JS/Desktop/'Untitled CD.fpbf'/thefile.gz"

or

do shell script "cp -p "&"/Users/JS/Documents/thefile.gz"&" "&"/Users/JS/Desktop/\"Untitled CD.fpbf\"/thefile.gz"
__________________
Please respond, I am desperate for human contact!
raymondlewisjone is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:14 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.