Re: I've changed the name of a file - now how do I get at path to the new name?
Re: I've changed the name of a file - now how do I get at path to the new name?
- Subject: Re: I've changed the name of a file - now how do I get at path to the new name?
- From: Richard 23 <email@hidden>
- Date: Fri, 3 Nov 2000 02:02:53 -0800
>
I've got a variable (vPathName) that contains a reference (i.e. path) to
>
a file:
>
>
--> file "Work:DMS Group:Database:Test Images:xxx.jpg"
>
>
I can change the name of the file by:
>
>
set the name of vPathName to "NewFileName"
>
>
great, however the variable still contains the path to the old file name.
>
>
How do I get the path to the newly renamed file?
>
>
I need it to put back into the database (FileMaker) where I got the
>
original pathname, so that it will point to the correct (i.e. renamed)
>
file.
Here's a couple of handlers (and a property) I rarely do without:
(obfuscation fans should enjoy bits of this)
property |t/d|: a reference to AppleScript's text item delimiters
--
---------------------------------------------------------------------------
-----
-- extracts name from path string
--
---------------------------------------------------------------------------
-----
on GetName(filePath)
set {|t/d|'s contents} to {":"}
set filePath to filePath as string
set {|t/d|'s contents} to {"", filePath's text item |?|(filePath ends
with ":", -2, -1)}
result's end
end GetName
--
---------------------------------------------------------------------------
-----
-- splits path string to {path to parent folder, name}
--
---------------------------------------------------------------------------
-----
on SplitPath(filePath)
set {|t/d|'s contents} to {":"}
set filePath to filePath as string
tell filePath to if it ends with ":" then set filePath to text 1 thru
-2
if (count filePath's text items) = 1 then set filePath to filePath &
":" & filePath
tell filePath's text items to set {|t/d|'s contents} to {"", items 1
thru -2 & "" as string, end}
rest of result
end SplitPath
--
---------------------------------------------------------------------------
-----
-- poor man's ternary, useful only if expressions are short and fast
because both
-- are evaluated before this handler actually is called
--
---------------------------------------------------------------------------
-----
on |?|(|boolean|, ifTrue, ifFalse)
log |boolean|
if |boolean| = true or |boolean| is not equal to false then
ifTrue
else
ifFalse
end if
end |?|
Given that you have these handlers in the script you could then get the
new path with:
set {folderPath, oldName} to SplitPath(vPathName)
tell application "Finder" to set the name of alias vPathName to
newFileName
set vPathName to folderPath & newFileName
or if you don't do this sort of thing very often and don't need the
handlers:
set AppleScript's text item delimiters to ":"
if vPathName ends with ":" then set vPathName to vPathName's text 1 thru
-2
tell application "Finder" to set the name of alias vPathName to
newFileName
set vPathName to vPathName's text items 1 thru -2 & newFileName as string
set AppleScript's text item delimiters to ""
That should cover it.
Look for exciting methods like this and many others on my little
AppleScript
site:
http://homepage.mac.com/richard23/
Of the "One Step Beyond" site, AppleScript luminary Walter Ian Kaye
beamed:
"Is it some kind of Zen puzzle? Please fix."
R23