batch renaming items in subfolders with apple script - applescript

I have tried all day now to do the following:
I have a folder with 99 subfolders. each subfolder has a pdf file inside and they all have to be renamed to carry the same name. now they are named with continuous numbers.
I have been doing all my stuff with the automator as I am a novice to scripting. but i kinda don't like giving up on a problem. googling did not bring any good solutions.
i have tried to make sense of example scripts and amend them, but had no success.
a short try was:
tell application "Finder"
set selected to selection
open selected
get files of folders of selected
set name of files of folders to "anschreiben"
end tell
I have also tried:
tell application "Finder"
set selected to selection
open selected
set mlist to every folder of selected
set current_folder to first item of selected
set xxx to first item of current_folder
set name of xxx to "yyy"
repeat with this_folder in mlist
open current_folder
set item 1 to item of current_folder
end repeat
end tell
, but this renamed the first folder to yyy and produced and error that the file name is already given (because it renames the subfolders, not the files inside)
SO...how do I get on level deeper and rename all the files in the subfolders??
Thanks guys, I know it's probably easy for you.

Look for "entire contents"
set newName to "New.pdf"
tell application "Finder"
set myFiles to every file of (entire contents of (first item of (selection as alias list)))
repeat with aFile in myFiles
set aFile's name to newName
end repeat
end tell

it is done! Thanks to you and one or two other pros I now have a beautiful bulk mailing script routine using automator, a bash line and (mainly) applescript. I use it for job applications but you can use it for any case where you want individualised bulk emailing with Mail, MS Word and any given list of contacts in Excel (or Address Book for that matter). For the sake of being complete I will add all necessary steps. with any given list of x names, email addresses, personal addresses you can generate x subfolders, containing x personalized letters and not-personalized documents. once you start the last script and select the folder you can watch mail sending them all out, addressing the person by name and attaching the right personalized letter (you were involved in this part, adayzone!)! It corrects for foreign name spelling that is rendered differently in the email address. It works best for email addresses using the last name before the "#" and can now ignore the first name if it is set in front of the last name (i.e. firstname.lastname#company.com). Thank you all very much for the assistance! this was great team effort.
I shall post it as soon as I am home, should I add it in here or in another forum (for sharing code)?

Related

How to use Automator and Applescript to batch move files/folders to a specified folder based on specific text in Calendar Events, MacOS

Every day, I get up to 25 real estate photoshoots from a photographer, edit them, and export them into their respective realtors' folder in Dropbox. I get each shoot from the photographer in a folder named with its respective address/location. Currently, after editing each shoot, I look at the foldername (the address/location), go to the Calendar app, find which event has that location, see who the respective realtor is, navigate to their personal folder or create it if it doesn't exist, and then export the photoshoot into their folder (the realtor's name). Every. Single. Time.
I'm trying to automate this process by simply exporting all the shoots into one folder, triggering an Automator Folder Action which sorts the shoots into their respective realtor's folder, based on the foldername.
The relationship between the source folders and destination folders is determined based on location data from a Calendar Event. I've already figured out how to pull the data from Calendar, but getting Automator to move the folders around using the data has me thoroughly stumped.
I've tried several things in Automator, and I'm sure there's a simple way using Applescript but I haven't learned enough about Applescript to make it work. I've tried copy/pasting bits of Applescript code from other posts but I've finally thrown my hands up and decided to ask the experts lol. Here are some screenshots to help illustrate the issue.
Folders full of pictures "Test"(Sources):
"Realtors" Folder (Destinations)
Here is the Automator Workflow I've created so far, which pulls the address (Source foldername) and realtor names (Destination foldername [with other junk to be removed]) from Calendar event data, and creates 2 txt files (shown in the screenshot after this one):
Text files created by above workflow:
It's possible that I'm overcomplicating this horribly, but the solution that comes to mind is this: The text files correlate based on line number, i.e. line 1 of "Today's Locations.txt" (Location: 326 Whiterock Dr) corresponds with line 1 of "Today's Shoots.txt" (Summary: Photos for Remy Locasio).
When I drop these folders into the trigger folder, "Test" in this case, Automator/Applescript could search the "Today's Locations.txt" file for "326 Whiterock Dr" and say, "Hey, that's on line 1. I'm gonna go into "Today's Shoots.txt",find line 1, parse out (Summary: Photos for ), take the realtor's name only (Remy Locasio) and move the input folder ("326 Whiterock Dr") into the realtor's folder with that name ("Remy Locasio"), then do it again for the next folder and it's corresponding documents' line, etc etc, until all the folders are moved.
Before the realtor's name is typically the word "for ". Whether it's Photos for , Photos and videos for , Photos and aerial photos for etc. so "for " could be a delimiter maybe?
Sometimes, after the realtor's name, there's a note, always separated by a " (", eg. Brandi Smith (text when on the way), so " (" could be another delimiter maybe?
Sometimes the realtor's name is spelled wrong in the calendar but not in the folder, as is the case with Brandi vs Brandy Smith on line 9. There would need to be a popup in this case asking for the correct folder. Something to the effect of "Could not find a folder called "Brandi Smith". Should we create it or choose another folder? and I'd choose the correct folder, or in the case of "Megan Forsberg" (line 8) who doesn't have a folder yet, allow it to create the folder.
I've racked my brain for a week trying to do this with Automator and variables and I'm at a complete loss, any help would be a godsend. I'll gladly clarify anything that doesn't make sense or answer any questions anyone has.
I can't understand the whole problem clearly but you can use below code in the applescript for the regex part.
set a to "Photos for Karie Williams (Zonker)"
set b to do shell script "str='" & a & "';sed 's/(.*//g' <<< $str"
set c to do shell script "inp='" & b & "';sed 's/.*\\(for \\)//' <<< $inp"
display dialog c

Running into problem when using choose folder in AppleScript

So I have a folder with many files whose count changes regularly and I need to count them using AppleScript. I had a script like this in place at first:
tell application "Finder" to return count of (every file of (choose folder))
Later, while researching, I found something like this:
tell application "Finder" to return count files of (choose folder)
However neither seems to work. I can't use System Events either since the choose folder just results in an endless run loop when used inside a System Events-tell block.
The error I get is:
"Finder got an error: Expected a reference." number -1727
If you are looking to get just the count of files within the selected folder and not subfolders or files within subfolders, then use:
tell application "Finder" to return count files of container (choose folder)
If you want the full count of items within a folder, then use:
tell application "Finder" to return count items of entire contents of container (choose folder)

Apple script to delete files with custom label (mavericks)

I'm trying to create a simple script to delete files based on a custom label I've already assign.
I'm currently trying to limit the search for the script to a test folder, but ultimately I want the script to search in all the user folder and get all the files from several different locations. I may need authentication for the process.
But so far I have this
tell application "Finder" delete (every item of folder
"/users/ro/documents/Erase test" whose label is "test") end tell
and I get this error
error "Finder got an error: Can’t get folder
\"/users/ro/documents/Erase test\"." number -1728 from folder
"/users/ro/documents/Erase test"
As I said I don't really know much about scripts, so I don't know all the terms but I hope someone can point me in the right direction.
Saw this late.
Tested this on 10.6.8 and will jump on a Mavericks machine to test, but this should work:
set f to choose folder
tell application "Finder"
delete (every item of f whose label index is 1)
end tell
A few notes about your attempt:
1) AppleScript doesn't 'natively' understand POSIX paths (but coercion to/from is possible), so (as I have it) "choose folder" returns what is known as an alias (not to be confused with a string -- but again, coercions to/from strings/aliases are simple).
2) note that the label is recognized as "label index", which is an integer.
3) you could/should test by taking out "delete" in that line to return a list of those items.
[edit] yes, this is fine on Mavericks.

BibDesk script to link Skim annotations to citation entry

Some Background: I've used Skim with BibDesk for a while now for reading and annotating scientific journal articles. Recently, I purchased an android tablet and would like to use it to read and annotate .pdfs as well. I use the reference library Eratosthenes with ezPDF Reader and sync all files through Dropbox. The issue I'm having is that Skim stores annotations as extended attribute files by default, which are not accessible to other devices through Dropbox. I've worked around this problem by saving the annotations as .fdf files and then linking the .fdf files to the citation entry in BibDesk. ezPDF reader can import .fdf files as annotations, and if the annotation file is linked to the BibDesk entry, both the .pdf and .fdf can be easily downloaded to the tablet without needing to sync the entire Dropbox folder full of hundreds of references.
I'd like to write an applescript that does this automatically, but am very new to applescript and am having a hard time getting started. I've written the following script to be executed when "command-s" is pressed while in skim:
tell application "Skim"
set docPath to path of front document
set notesPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in notesPath as "Notes as FDF"
end tell
This essentially saves the current document while simultaneously exporting an .fdf file. Within the same script, I would like to link the .fdf file to the appropriate citation entry in BibDesk. This would involve:
determining the citation entry name associated with the .pdf (maybe search through entries to locate one linked with the front document)
check to see if the .fdf file is already linked to it
if not, attach .fdf file
I haven't been able to find someone who's done something similar, and really can't get past the first step. I tried writing something basic (assume citation entry is highlighted, assume .fdf file is not linked), which produces no results:
tell application "BibDesk"
set thePub to selection
tell thePub
set theFieldName to "Local-URL-2"
set value of field theFieldName to fdfPath
end tell
end tell
Is anyone familiar with Bibdesk applescripts able to help me with the second part of this code?
Thank you very much in advance.
I'm not too familiar with scripting BibDesk, but I've been poking around a little. Heavily commented to help guide you:
set theFieldName to "Local-URL-2"--generally better not to put
--something like this in a tell block if it isn't necessary
tell application "BibDesk"
--as you have it, you are simply putting the selection class
--into a variable. here we reference the specific selection
--object of the document object:
set thePubSel to selection of document 1
--and, since this returns a *list* of pubs, I'm grabbing just the first item
--(but a loop iterating through every publication in the selection perhaps better)
set thePub to item 1 of thePubSel
tell thePub
set value of field theFieldName of it to fdfPath
end tell
end tell
The following code answers my initial question satisfactorily. The first section reiterates the saving and exporting commands. The second section locates the citation entry containing the linked .pdf file (front document in Skim). The third section attaches (links) the .fdf file to the citation entry (thanks to CRGreen for some help here).
--save and export .fdf file
tell application "Skim"
set docPath to path of front document
set fdfPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in fdfPath as "Notes as FDF"
end tell
--search for relevant citation entry
tell document 1 of application "BibDesk"
--sort all publications in library by Cite Key
set thePubs to (sort (get publications) by "Cite Key")
-check each publication individually (surely this is not the most efficient way to do this)
repeat with aPub in thePubs
--check to see if the .pdf is in the citation entry
tell aPub
if linked files contains (POSIX file docPath) then
set thePub to aPub
--once the citation is found, exit loop
exit repeat
end if
end tell
end repeat
--link the .fdf file to the citation entry (if it isn't already)
tell thePub
--if the fdf file exists in the linked file, do nothing
if linked files does not contain (POSIX file fdfPath) then
add (POSIX file fdfPath) to end of linked files
end if
end tell
end tell
I'm assuming that there is a better way to search for the citation entry with the associated .pdf file (maybe using the applescript search command?). This solution works for me, but if you know of a more elegant way to solve this problem, feel free to mention it.
To map the script to a keyboard shortcut ("command-s" is convenient), see here (the menu title is the name of your script). The script needs to first be saved to the ~/Library/Application Support/Skim/Scripts directory.
I'm just learning applescript, and I realize that this may have been a very trivial exercise for most, but perhaps it will help out another beginner.

AppleScript error when trying to copy home folder to shared drive

So, i'm working on a script that copies the home folder to a mounted shared drive folder. But i'm getting the following error:
"Handler can't handle objects of this class number -10010"
This is the code I came up with following the example of other code i've seen on here. I'm guessing that it is the way i'm telling finder to duplicate.
set vserver to ("/Volumes/sharedfolder")
set source to ("/Users/user")
tell application "Finder"
duplicate source to vserver
end tell
How else can I write this?
I've also tried running a boolean test to see if Finder saw the shared folder or my home folder and it retured false. (but only one false when it should have returned two)
tell application "Finder"
setaBoolean1 to get (exists vserver)
setaBoolean1 to get (exists source)
end tell
set vserver to ("/Volumes/sharedfolder")
The line above sets the variable vserver to a string object consisting of "/Volumes/sharedfolder". Likewise, the set source to "/Users/user" line sets source to a string object containing "/Users/user". Note that strings are not what the Finder is expecting when you're telling it to duplicate items.
The tell app Finder line is basically trying to tell the Finder to duplicate one string into another string, which it doesn't know how to do (hence the Handler can't handle objects of this class message).
What you need to do is to, instead of creating strings, create some sort of file system reference to those folders, so that the Finder knows how to deal with them.
There are numerous ways to do this, but the method I found that works (which uses the same POSIX style path format) is the following:
set vserver to POSIX file "/Volumes/sharedfolder"
set source to POSIX file "/Users/user"
tell application "Finder"
duplicate source to vserver
end tell

Resources