Hello everyone, first time poster, new to AppleScript. Glad to have such a great resource available in this website!
I’ve searched and searched these forums and the net, and would like some clarification on this subject. Am I correct in understanding that there is no way to get the path of an already open file? For example, if I open a file located on a remote volume, can AS return the full path to that file after it’s open?
Here’s what I want to do: A user opens a file and modifies it. I want to supply a script that will rename the file and move it to another location as part of an editorial workflow. The idea is to eliminate any confusion in where the file needs to go, how to get it there, and how to do so without creating duplicates.
AS doesn’t seem to be able to move a file without knowing its current location, and the only way I could see in my research is to have the user “choose” a file, so AS can get the path. Am I missing something?
tell application "TextEdit"
--The first several lines count characters and create an estimate for column inches in a 6-column newsprint layout
set NumChar to count (characters of text of document 1)
set colInch to (NumChar / 220)
end tell
set colInch2 to RoundDecimal(colInch, 2, up)
on RoundDecimal(NumberToRound, DecimalPlace, UpDown)
set RoundFactor to 10 ^ DecimalPlace
NumberToRound * RoundFactor
round result rounding UpDown
result / RoundFactor
end RoundDecimal
--here's where the good stuff starts
tell application "TextEdit"
tell the front document
set myName to name as text --gets the name of the current document
set thePath to (POSIX file path of myName) as text --this gets the path
set firstPart to text 1 thru 2 of myName as text --the next three lines rename the file
set lastPart to text 3 thru end of myName as text
set newName to (firstPart & "-" & colInch2 & "in -" & lastPart) as text
save
close
end tell
end tell
set origPath to thePath --the goal is to rename and move the file, so first set new variable with current path
set findIt to myName --finds file name in the path
set replaceItWith to newName --replaces file name in that path with new file name, thus renaming the file
set thePath2 to searchReplace(origPath, findIt, replaceItWith) --calls the searchReplace routine, sets the new path/file name
on searchReplace(theText, SearchString, ReplaceString)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's text item delimiters to oldDelims
return newText
end searchReplace
tell application "Finder"
set name of file thePath to newName --tells Finder to rename the file
set destFolder to "Editorial:Copy for Richard:~ready for pagination" --maps out the path to a network volume
move file thePath2 to folder destFolder --moves the newly renamed file
try
delete file thePath2 --deletes original file, which only exists if user moves a file between volumes, which by default only copies a file. If the file doesn't exist, no error is created.
end try
end tell
This works great from my machine, but on others whose permissions are different, it doesn’t fly. Based on the above, could anyone tell me the minimum required permissions to make it work? The particular machine I’m talking about also has managed preferences. Are there any particular applications that must be allowed in order for this to work?
Thanks!
Paul
Model: G4 1.25GHz
AppleScript: 2.1.1
Browser: Firefox 1.5.0.1
Operating System: Mac OS X (10.4)