to:
1234-5_ParentRandomName_BKCVR_SQ2014.indd
1234-5_ParentRandomName_SQ2014.indd
1234-5_ParentRandomName_CVR_SQ2014.indd
The Folder Structure is like this:
1234-5_ParentRandomName_SQ:1234-5_indesignfolder_SQ2014:1234-5_backcover_SQ2014.indd
I wrote script that changes the name 1234-5_backcover_SQ2014.indd to 1234-5_RandomParentName_SQ2014.indd but I can’t get it to actually rename the document file. Can anyone help? I"m still new to Actionscript.
Here is the script I wrote:
tell application “Finder”
set theTexxxt to selection
set theText to (theTexxxt as text)
set AppleScript’s text item delimiters to “backcover”
set theTextItems to text items of theText
set theSuffix to text ((offset of “" in theText) + 1) thru -1 of theText
set thePrefix to text 1 thru ((offset of "” in theSuffix) - 1) of theSuffix
set AppleScript’s text item delimiters to thePrefix
set theTexxt to theTextItems as string
set AppleScript’s text item delimiters to {“”}
theTexxt
set name of file to theTexxt
end tell
Hi. Welcome to the forum and to AppleScript.
In your provided code sample, there is no target file to rename and it’s unclear what a target should be renamed.
Using less similar, more descriptive variable names might help you see the problem(s). You also have both offset and text item delimiters, which are similarly used, so one of them can go.
This may help get you further:
tell application "Finder"
set myTarget to selection as alias
set nameString to myTarget's name
set thePrefix to nameString's text 1 thru (offset of "_" in nameString)
set parentName to myTarget's container's name
set AppleScript's text item delimiters to "" --default value is enforced for upcoming reversal
set theSuffix to nameString's text -((offset of "_" in nameString's text items's reverse as text)) thru -1
set myTarget's name to {thePrefix, parentName, theSuffix} as text
end tell
Interestingly enough, I just joined these forums to ask a very similar question. Marc Anthony, your answer was most helpful in my figuring out how to get about 95% there. In my specific case, I’m trying to integrate a rename-according-to-container AppleScript into an Automator workflow.
I work at a radio station and every time we add music to our digital library, I have to archive it first. I let iTunes hold the files and take care of the organizational aspects (artist folders containing album folders containing song files), and then when the time comes I zip the album folder, rename it according to a scheme of “Artist - Album.zip,” and transfer it to a directory on a local external hard drive. I had set up an Automator workflow using the Create Archive action, followed by Add Text to Finder Item Names (where I’d type "The Beatles - " or whatever it may be, completing the desired name of the archived file), and finally Move Finder Items, which would put the zip file in our Archived Music folder. The problem with this, as you can imagine, is all the manual entry of the artist names – dealing with one or two albums, it’s not so bad, but if I’m archiving one after another it’s a total drag. (And Automator gives you no indication of which file you’re applying the prefix to, so I’ve set up 4 or 5 in a row only to find that some compressed faster than others and the wrong artist names ended up on the wrong albums.
Based on Marc Anthony’s example script above, I put this together (I’m pretty new to Applescript, so let me know if I’m including some extraneous lines here):
tell application "Finder"
set myTarget to selection as alias
set nameString to myTarget's name
set Archive to nameString's text
set parentName to myTarget's container's name
set AppleScript's text item delimiters to ""
set myTarget's name to {parentName & " - " & Archive} as text
end tell
Of course, this assumes that a file is selected, and as far as I can tell, when the Finder gets done archiving a folder, the archive is unselected. So my question is twofold:
is my script above generally on target? I run it on zip files and it seems to do the trick.
how do I get this to integrate with my Automator workflow so that it’s renaming the recently-created zip file and then leads into the Move Finder Items step of the process?
Hi, BK, and also welcome. I’m going to let someone else field the Automator portion of your question; I don’t use it and find it incomprehensible. You might actually want to post that question in the Automator forum.
If you know where a file lives, it’s always preferable to not use a selection. namestring already holds the value you assigned for Archive, but you can change the variable to read to your preference.
tell application "Finder"
set myTarget to (choose folder)
set nameString to myTarget's name
set parentName to myTarget's container's name
try
set myTarget's name to parentName & " - " & nameString
on error --could be too long, already in use, etc.
set myTarget's name to parentName & " - " & (display dialog "" default answer "Enter another name for the archive." with title "That's not going to work.")'s text returned
end try
end tell
Thanks for the welcome and for the advice. It dawned on me that maybe trying to integrate Automator and AppleScript for this was more trouble than it’s worth, since the only step prior to this rename is to compress the folder into a zip archive, and maybe AppleScript can take care of that on its own. I looked into it around the forum, but it seems that AppleScript can’t tell the Finder to zip something, but can use a shell script to do so? Is this correct? It’s starting to go a little over my head.
Your script worked great, though it creates a new step for me: though I no longer have to manually rename the zip file, I now have to navigate to it before the script can run. My ideal solution would run like this:
I select a folder (in this case, an album stored by iTunes) and start the script, which archives the folder into a zip file
The resulting archive file is then renamed based on its parent folder name, ie “Revolver.zip”, located in parent folder “The Beatles”, becomes “The Beatles - Revolver.zip”.
The now-renamed zip file is moved (not copied) over to a folder called Archived Music, with no copy left behind in the original directory, and we’re done.
I think I can probably work out the parts individually, but my theorizing breaks down because I can’t figure out how to point the script toward the newly created archive file after step one. I don’t want to have to navigate to it via a dialog box; is there a way to tell the script to
select file "nameString & ".zip""
or something along those lines to shift its focus from the original selection to this new file?
It just occured to me that Automator may have a compression action, well, here is a way to use the built in compression utility by applescript anyway. This one assumes that only one folder is selected before it is run, and performs no checking of errors.
tell application "Finder"
activate
set fol2arc to POSIX path of ((selection as alias) as text)
set cmd to quoted form of POSIX path of ((path to application id "com.apple.archiveutility" as text) & "Contents:MacOS:Archive Utility")
end tell
set fn to text 1 thru -2 of fol2arc
do shell script cmd & " " & quoted form of fol2arc & " ; mv " & quoted form of (fn & ".cpgz") & " " & quoted form of (fn & ".zip")
So I figured really, that it would be unnecessary in that case, but you are right, it isn’t much robust!
This should be better!
tell application "Finder"
activate
set failed to false
try
set a to selection as alias
set b to name of folder a
set fol2arc to POSIX path of (a as text)
set cmd to quoted form of POSIX path of ((path to application id "com.apple.archiveutility" as text) & "Contents:MacOS:Archive Utility")
on error
set failed to true
end try
end tell
if failed then
tell application "SystemUIServer"
try
display dialog "For this to work you must select one and just one folder"
end try
end tell
error number -128
end if
set fn to text 1 thru -2 of fol2arc
do shell script cmd & " " & quoted form of fol2arc & " ; mv " & quoted form of (fn & ".cpgz") & " " & quoted form of (fn & ".zip")
By the way Stefan, do you know which archieve scheme that the Archieve utility uses?
set yourFolder to quoted form of (POSIX path of (path to desktop as text) & "Archived Music")
tell application "Finder"
set folderAlias to selection as alias
set parentAlias to container of folderAlias as alias
set folderName to name of folderAlias
set {parentPath, parentName} to {(POSIX path of parentAlias), name of parentAlias}
end tell
set zipName to (parentName & " - " & folderName & ".zip" as text)
do shell script "cd " & quoted form of parentPath & "; zip -r " & (quoted form of zipName) & space & folderName
set zipFile to quoted form of (parentPath & zipName as text)
do shell script "mv " & zipFile & space & yourFolder
I added the moving of the folder to the archieve as well, here it must be edited in the name of the archive.
tell application "Finder"
activate
set failed to false
try
set a to selection as alias
set b to name of folder a
set c to name of container of a
set fol2arc to POSIX path of (a as text)
set cmd to quoted form of POSIX path of ((path to application id "com.apple.archiveutility" as text) & "Contents:MacOS:Archive Utility")
on error
set failed to true
end try
end tell
if failed then
tell application "SystemUIServer"
try
display dialog "For this to work you must select one and just one folder"
end try
end tell
error number -128
end if
set fn to text 1 thru -2 of fol2arc
do shell script cmd & " " & quoted form of fol2arc & " ; mv " & quoted form of (fn & ".cpgz") & " " & quoted form of (fn & ".zip")
tell application id "com.apple.archiveutility" to quit
tell application "Finder"
set thezip to a reference to POSIX file (fn & ".zip")
set name of file thezip to (c & " " & b & ".zip")
move file ((container of a as alias as text) & (c & " " & b & ".zip")) to folder hfsPathtoArchievesFolder
end tell
To get the correct hfspath; select your archieves folder and run this script:
tell application "Finder"
activate
try
set a to selection as alias
set b to a as text
set the clipboard to ("\"" & b & "\"") as text
end try
end tell
Then paste it over the name : hfsPathtoArchievesFolder in the end of the script.
Well, I saw that the sizes differed, and I was to lazy to figure out exactly which compression scheme zip used, not even sure if zip was used, Archieve Utility is a front end. So I opted for a simple solution. I also like to see that something happens, and you can see that with archieve utilty!
Thanks so much for the help! I think we’re almost there for what I need to do. This is going to be able to save me so much typing in the long run!
adayzdone, I tried your script and discovered a couple things:
It hits an error if the selected folder has more than one word in its name
It sends the file to the desktop; if there’s an Archived Music folder there, it puts it in there exactly as I’d want it. If there isn’t, it names the resulting zip file “Archived Music.” I bring this up because ultimately, the Archived Music folder is going to live on an external drive, and I’m not sure I know how to specify the path to that directory in the same way that McUsr’s script does… any help would be appreciated.
McUsr, your script also does the trick, but right now it seems to compress to a combination of zip and cgpz, which means that when I go to unzip the file, it gives me another compressed file (without an extension, which could be confusing to any colleagues I might need to share the archived file with), which I need to decompress all over again to get to my original files. I’d prefer if it compressed directly to zip format without an extra layer of cgpz. I admit I don’t know enough about shell scripts to get it to do only zip (and thus only one level of compression) – I’ve tinkered with the script you provided, but nothing I did ran all the way through without errors. But if I could work that out, it would keep things simpler for me later – any slight advantage of increased compression on two levels isn’t worth it to me.
It seems you’ve both provided different ways to achieve what I need, and I’m thinking that the solution might be somewhere in between. This is very exciting though! I really appreciate your help.
I missed a “quoted form of”, the script below should do the trick.
set yourFolder to quoted form of (POSIX path of (path to desktop as text) & "Archived Music")
tell application "Finder"
set folderAlias to selection as alias
set parentAlias to container of folderAlias as alias
set folderName to name of folderAlias
set {parentPath, parentName} to {(POSIX path of parentAlias), name of parentAlias}
end tell
set zipName to (parentName & " - " & folderName & ".zip" as text)
do shell script "cd " & quoted form of parentPath & "; zip -r " & (quoted form of zipName) & space & (quoted form of folderName)
set zipFile to quoted form of (parentPath & zipName as text)
do shell script "mv " & zipFile & space & yourFolder
An easy way to get a path is to drag the folder into an open AppleScript Editor.
In this example, change
set yourFolder to quoted form of (POSIX path of (path to desktop as text) & "Archived Music")
to
set yourFolder to quoted form of
and then drag your target folder to the end of the line… after “of”. Also, make sure you enclose the path in quotes. “path/to/your/folder”
The script is now reworked to give exact the same compression as archieve utility, it is not so cool anymore, showing the compression though.
tell application "Finder"
activate
set failed to false
try
set a to selection as alias
set b to name of folder a
set c to name of container of a
set fol2arc to POSIX path of (a as text)
set cmd to "ditto -c -k --sequesterRsrc --keepParent "
-- http://stackoverflow.com/questions/7748027/use-archive-utility-app-from-command-line-or-with-applescript
on error
set failed to true
end try
end tell
if failed then
tell application "SystemUIServer"
try
display dialog "For this to work you must select one and just one folder"
end try
end tell
error number -128
end if
set fn to text 1 thru -2 of fol2arc
do shell script cmd & quoted form of fol2arc & " " & quoted form of (fn & ".zip")
tell application "Finder"
set thezip to a reference to POSIX file (fn & ".zip")
set name of file thezip to (c & " " & b & ".zip")
move file ((container of a as alias as text) & (c & " " & b & ".zip")) to folder hfsPathtoArchievesFolder
end tell
At the moment I am unsure about how the automator action for compressing files works, but if it uses the archieve utility, with the visual display, then I’m quite open for making a workflow with you!
What do you know. Having a break, I wanted to play a little bit more with this, not giving up on making a zip archieve with the gui just yet, and I typed wrong, trying to specify the path to the Archieve Utility.app from the command line. But I found a preference pane! So now I can make Archieve Utility.app make zip files directly!!!
I want to add, that it is generally good to see some progress of some kind, when doing lenghty operations, and compressing a music CD, now that is a lenghty operation, even on my SDD disk! And that is the good thing about Archieve Utility.app from my point of view, it is not good to use if you are to share archieves with users of other operating systems though.
So, in order to make Archieve Utility.app generate zipfiles as defaults enter the command below in a terminal window and hit enter:
open /System/Library/CoreServices/Archive\ Utility.app/Contents/Resources/Archives.prefPane/
Go into the Preference pane, and change the default archieving format from cpgz to Zip.
That preference pane renders the script more or less useless, as you in fact can make Archieve Utility.app take care of most of the chores for you directly! But I have changed the script, so it works with the new information.
The script doesn’t quit Archieve Utility.app anymore, as you may start the archieval of several music CD’s in sequence.
tell application "Finder"
activate
set failed to false
try
set a to selection as alias
set b to name of folder a
set c to name of container of a
set fol2arc to POSIX path of (a as text)
set cmd to quoted form of POSIX path of ((path to application id "com.apple.archiveutility" as text) & "Contents:MacOS:Archive Utility")
on error
set failed to true
end try
end tell
if failed then
tell application "SystemUIServer"
try
display dialog "For this to work you must select one and just one folder"
end try
end tell
error number -128
end if
set fn to text 1 thru -2 of fol2arc
do shell script cmd & " " & quoted form of fol2arc
tell application "Finder"
set thezip to a reference to POSIX file (fn & ".zip")
set name of file thezip to (c & " " & b & ".zip")
move file ((container of a as alias as text) & (c & " " & b & ".zip")) to folder hfsPathtoArchievesFolder
end tell
I read about problems with opening Os X archieves on other machines here: Beyond .zip: Secrets of the Archive Utility - Macworld Forums and I have experienced that myself. I read up on this while fiddling with the Archieve Utility, and today I made this script that removes all the files that gives trouble on a other platforms (MsWindows).
It does not make a backup of the archieve before it extracts the __MACOSX/ tree in the archieve, or removes the ._DS_Store files.
You have to select at least one archieve in the frontmost finder window for it to work.