Can't set name of folder

I’m pretty new to applescript. Can anyone tell me why this won’t work? I want it to copy a folder from a SD card to the desktop, rename it to the date (10_21_2004) and then put the folder into the pictures folder. The hang up thus far is the renaming part.

set copy_source to contents of “SANVOL:DCIM:100PENTX”
set old_file to “Desktop:100PENTX”
–set the_date to date string of (current date) as text
set copy_target to “eMachado:Users:Socram:Pictures:new:”

set today_ to current date
set day_ to day of today_
set month_ to month of today_ as number
set year_ to year of today_
set the_date to (month_ & "" & day & "" & year) as text

with timeout of 999 seconds
tell application “Finder”
duplicate copy_source to desktop
set name of folder old_file to (“Desktop:” & the_date)
move (“Desktop:” & the_date) to copy_target with appending
end tell
end timeout

any help is greatly appreciated.
marcos

sorry forgot to say, the error i get is "Finder got an error: Can’ set name of folder “Desktop:100PENTX” to “Desktop:10_21_2004”. " thanks.

There are two basic errors in your script.

First, when you change the name of an object, you just specify its new name, not the entire path to the item. So when you want to change the name of a folder on the desktop you omit the ‘Desktop:’ part in the new name:

So instead of:

 set name of folder old_file to ("Desktop:" & the_date) 

you should just:

 set name of folder old_file to ( the_date) 

Secondly, when you move items, your command:

 move ("Desktop:" & the_date) to copy_target with appending 

won’t work because you’re trying to move a string - “Desktop:” & the_date equates to a string. You need to specify it as a folder:

move folder ("Desktop:" & the_date) to copy_target with appending

thanks I’ll give that a shot, especially the moving folder stuff. i swear I’ve tried

 set name of folder old_file to ( the_date) 

before. I think I tried that before I started adding the “Desktop:” stuff (thinking it might be needed.

Thanks again, I’ll be back if it still doesn’t work…

Marcos

Ok, still having problems. The same "Can’ set name of folder “Desktop:100PENTX” to “Desktop:10_21_2004” error. The code is now as follows, thanks to Camelot’s advice.


set copy_source to contents of "SANVOL:DCIM:100PENTX"
set old_file to "Desktop:100PENTX"
set copy_target to "eMachado:Users:Socram:Pictures:new:"

set today_ to current date
set day_ to day of today_
set month_ to month of today_ as number
set year_ to year of today_
set the_date to (month_ & "_" & day_ & "_" & year_) as text


with timeout of 999 seconds
	tell application "Finder"
		duplicate copy_source to desktop
		set name of folder old_file to (the_date)
		move folder ("Desktop:" & the_date) to copy_target with appending
	end tell
end timeout

Any ideas why i still get this error?
thanks-

So I think the problem here is that “name” is not a property of “folder” like it is a property of a “file”. (Reference the Standard additions library for file properties, and the System Events library for folder properties) So I can’t


set name of folder x to y

because name isn’t a property of a folder. So how do you rename a folder through applescript? Why am I having so much trouble finding the answer to this?

Marcos

Hi,

When you first learn AppleScript, it’s good to know how to reference items. A useful command is the standard additions scripting addition command ‘path to’. Using this command, you can get a reference to the desktop. For instance, on my computer:

path to desktop

gives me the alias reference (or name reference):

alias “Macintosh HD:Users:kel:Desktop:”

The path:

“Macintosh HD:Users:kel:Desktop:”

begins with your hard disk’s name and ends with the target folder. Here the target folder is the desktop. Another way to write this path when using the Finder is to use a Finder reference.

folder “Desktop” of folder “kel” of folder “Users” of startup disk

The Finder as well as the ‘path to’ command “knows” special folders. In the Finder, you could have written this:

tell app “Finder”
desktop
end tell

Note that ‘desktop’ is a property of the Finder and is not surrounded by quotes.

Using the ‘path to’ command you can get references to special folders such as the current users folder and I’m trying to find out the parameter for the Pictures folder if it exists.

gl,

Hi,

I found this in the folders.h header file in my system. The four letter code is “pdoc”. So, to get the path to the Pictures folder for the current user, one can do this:

path to “pdoc”

The result on my computer is:

alias “Macintosh HD:Users:kel:Pictures:”

So if I wanted a reference to a folder named “new” in my Users folder, I could do this:

set pic_folder to (path to “pdoc” from user domain) as string
set new_folder to (pic_folder & “new”) as file specification

Here, I used ‘as file specification’ because the folder “new” does not exist on my computer. If I did this:

set pic_folder to (path to “pdoc” from user domain) as string
set new_folder to (pic_folder & “new”) as alias

I would get an error. When you use an alias reference (or name reference) and the item doesn’t exist, you get an error.

The “&” character is used to concatenate strings as you know. If the first item is a string, then the concatenation will work. Sometimes, the concatenation will work even if it is not a string, but it is best to be consistant and always change the first item to string. Note the ‘as string’ coercer.

Editted: to find four letter codes for the ‘path to’ command on your osx system, use the Finder or whatever to search for this “folders.h” header file. Make a duplicate of this file and open with some text editor. Look for the codes which are self explanetory.

More Edit: there may be more than one “folders.h”. Look for the one in the System folder. The one in the Developer folder may not be current.

gl,

Well I figured it out. For those that care, the right code is below (there is some extra added since last time, but the problem described above has been solved as well):


set picture_frame to contents of "SANVOL:pictureframe"
set copy_source to contents of "SANVOL:DCIM:100PENTX"
set old_file to "Desktop:100PENTX"
set copy_target to "eMachado:Users:Socram:Pictures:new:"

set today_ to current date
set day_ to day of today_
set month_ to month of today_ as number
set year_ to year of today_
set the_date to (month_ & "_" & day_ & "_" & year_) as text

tell application "Finder"
	if item (picture_frame) exists then
		duplicate picture_frame to desktop with replacing
	end if
end tell

with timeout of 999 seconds
	tell application "Finder"
		duplicate copy_source to desktop with replacing
		set name of folder "100PENTX" of folder "Desktop" of folder "socram" of folder "Users" of startup disk to the_date
		move folder the_date of folder "Desktop" of folder "socram" of folder "Users" of startup disk to copy_target with appending
	end tell
end timeout


tell application "Finder" to eject "SANVOL"

so it looks like i just wasn’t been specific enough as to what i was trying to rename.

fyi, this is a script that i’m going to use on a picture frame i made from a powerbook. to add new pictures, i’ll just plug in a flash drive, which upon mounting will activate this script (via a preference panel: DSW). this script then copies the pictures to a new folder, and also copies files over to the desktop, if i’ve placed them in a “pictureframe” folder.

yo.
-m

Hi,

Note that the line:

set name of folder “100PENTX” of folder “Desktop” of folder “socram” of folder “Users” of startup disk to the_date

can be shorttened to:

set name of folder “100PENTX” of desktop

if you’re referencing the current user. This saves typing.

Way to go,

gl,