Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Applescript: how to rename a file?

Applescript: how to rename a file?
Thread Tools
AJM
Junior Member
Join Date: May 2001
Status: Offline
Reply With Quote
Oct 22, 2001, 02:41 PM
 
Hi,
sorry if this is trivial, but I am just starting out.
How do I make the Finder rename a file with a script? I can duplicate, move, but I can't find a way to change the name of a file (without using a shell command). Is there a way?
Thanks
AJM
     
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Oct 22, 2001, 10:16 PM
 
Originally posted by AJM:
<STRONG>Hi,
sorry if this is trivial, but I am just starting out.
How do I make the Finder rename a file with a script? I can duplicate, move, but I can't find a way to change the name of a file (without using a shell command). Is there a way?
Thanks
AJM</STRONG>
Well in OS 9 you would: set name of theFile to "The File".

Begin lecture mode
name being one of the propoperties of an item which file is derived from.
If you look in the finder dictionary you'll see.
And look for AppleScriptLanguageGuide.pdf on Apples' site.
End lecture mode
     
Forum Regular
Join Date: May 2001
Status: Offline
Reply With Quote
Oct 22, 2001, 11:18 PM
 
Alright, Mr. AppleScript smarty-pants (I grovel before your greatness...) explain this one...

I have a script that takes a URL, parses it down, and attempts to create a folder structure following the URL. Ie, if the URL is www.apple.com/developer/technotes/TN302.html, it will create a folder named technotes inside a folder named developer inside a folder named www.apple.com. (The idea being that then it would download TN302.html into that bottom folder.)

Unfortunately, the script is only able to create the top level one, then it fails with a useless error message of 'Finder error, could not make folder'. Gee, thanks for the info.

However, if I take the text from the event log window, and paste it into a new Script Editor window and run it, it can create the new secondary level folder just fine.

(Yes, I realize that some browsers (IE) have support for sucking down a whole website... I don't want that. I need specific control over what's downloaded when/where, and this seems like a trivial little problem that I can't seem to solve.)

Here's the code, more or less:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
on run
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {<font color = red>"/"</font>}
set browserURL to <font color = red>""</font>

tell application <font color = red>"OmniWeb"</font>
set browserURL to (address of front browser) as text
end tell

display dialog browserURL

set dirList to words of browserURL

-- Chop off http:
set dirList to items <font color = blue>2</font> thru (length of dirList) of dirList

set baseDir to <font color = red>"Mac OS X:Users:&lt;usernamegoeshere&gt; ocuments:"</font>

set dlDir to baseDir

tell application <font color = red>"Finder"</font>
repeat with folderName in dirList
set dlDirFull to dlDir & folderName & <font color = red>":"</font>
display dialog <font color = red>"Checking: "</font> & dlDirFull
if (not (folder dlDirFull exists)) then
display dialog <font color = red>"Making new folder "</font> & folderName & <font color = red>" at: "</font> & dlDir
make new folder at dlDir with properties {name:folderName}
end if
set dlDir to dlDirFull
end repeat
end tell

set AppleScript's text item delimiters to savedDelimiters

end run
</font>[/code]

[ 10-23-2001: Message edited by: Kickaha ]
     
Mac Enthusiast
Join Date: Aug 1999
Location: Calgary, Alberta, Canada
Status: Offline
Reply With Quote
Oct 23, 2001, 12:47 AM
 
You need to coerce dlDir into an alias for it to work correctly as a location reference for the make new folder command. So, you need to change this line:
&lt;blockquote&gt;&lt;font size=1 face="Geneva, Verdana, Arial"&gt;code:&lt;/font&gt;&lt;hr&gt;&lt;pre&gt;& amp;lt;font size=1 face=courier&gt;make new folder at dlDir with properties {name:folderName}&lt;/font&gt;&lt;/pre&gt;&lt;hr&gt;&lt;/blockquote&gt;

into this line:
&lt;blockquote&gt;&lt;font size=1 face="Geneva, Verdana, Arial"&gt;code:&lt;/font&gt;&lt;hr&gt;&lt;pre&gt;& amp;lt;font size=1 face=courier&gt;make new folder at alias dlDir with properties {name:folderName}&lt;/font&gt;&lt;/pre&gt;&lt;hr&gt;&lt;/blockquote&gt;

You were just missing the one coercion. Your script will work correctly now.

(edit: bad formatting)

[ 10-23-2001: Message edited by: King Chung Huang ]
     
Forum Regular
Join Date: May 2001
Status: Offline
Reply With Quote
Oct 25, 2001, 12:36 AM
 
Interesting, thanks... any idea why it always allowed the top level folder to be made though, and not subsequent folders?
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:32 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2