Renaming Files Sequentially as they are added to a folder using Automator or Applescript - applescript

I need to rename image files sequentially as they are added to a folder. ie. image-0001.jpg and image-0002.jpg are in a folder I add test.jpg and it is renamed image-0003.jpg. I have tried automators rename function but it will start over with image-0001.jpg each time a new file is added instead of continuing the sequence.
Any help is greatly appreciated.

You could make this easy on yourself by using a handy application built into the OS called "Folder Actions". Folder Actions contains one or more special handlers, formally known as folder action event handlers, that run when they are triggered by a change in the target folder. I know I'm confusing but I'll do my best.
What you are trying to accomplish requires an adding folder items to event handler. It requires one direct parameter, which can be anything you wish i.e. target_folder. The handler requires an additional parameter as well; after receiving, which should also be a variable name, i.e. these_items. I have composed a script for you that should do the trick. I have added comments that show you what I'm doing when I do it. Here it is:
on adding folder items to the target_folder after receiving these_items
tell application "Finder"
set all_images to every item of the target_folder as list
repeat with i from 1 to the count of these_items --iterates through all the items you dropped in the folder
set this_image to item i of these_items --the current image
set the name of this_image to "image" & (i + the count of all_images) as string --renames the image based on the number of images already in the folder
end repeat
end tell
end adding folder items to
YAY! The script is done! But are WE done? Not quite. We still need to attach the script to a folder (the script won't run if you try to execute it in script editor).
To do this, first save the script as a Script File in the Folder Action Scripts folder in the Scripts folder in either the local Library folder or the current user's Library folder. Create the folder yourself if it doesn't already exist. Next, launch the Folder Actions Setup application by double-clicking it in the AppleScript folder in the Applications folder. In the window that comes up, click the + button under the table on the left (click the "Enable Folder Actions" checkbox if it isn't already checked) to open a standard file browser sheet, navigate to your desired folder, and click "Open". The Choose a Script to Attach sheet automatically opens, listing all the scripts in all of the Folder Action Script folders. Choose the newly-created script, click "Attach", and BAM you are done!
To see the script in action, drag an image onto the folder. The image is instantly renamed, regardless of if the folder window is open. If you have any questions, or if the script doesn't work, just ask me. :)

well without digging through some code and handin you an answer I'll tell you want you want to do
is create a while loop that checks for the existence of image-000 & i where i is a variable of course and if it exists increment i then when the file doesn't exist rename your file.

Related

Run Applescript automatically when a PDF file is present within a folder

I have created an Applescript that requires to run automatically & repeat every time a pdf file is present within the folder on a server.
Look at the folder
File present?
Hold until file present
Activate script and complete
Look at folder - Repeat
Does anyone know where to start?

Mac Automator: Mass-create Subfolders

I am trying to use Automator to take a bunch of previously-created folders and create two new folders within each one, an "Originals" folder and a "Scans" folder. I would like to be able to drag or select the folders that need the subfolders and then have the action run automatically.
I've tried making both an action and a service to add the subfolders, but when I select multiple folders, it only runs once.
Here is a screenshot of what I've done so far...
Try saving the workflow as an Application:
Save the Application to your Desktop, then you can drop folders there whenever you want.
Here is an AppleScript that, when Exported as an Application, functions as a Droplet to make the desired folders in every folder dropped thereon:
on open droppings
repeat with everyDrop in droppings
if (info for everyDrop)'s folder is true then
--make two folders
tell application "Finder"
make new folder at (everyDrop as text) with properties {name:"Originals"}
make new folder at (everyDrop as text) with properties {name:"Scans"}
end tell
end if
end repeat
end open
on run
display dialog "Drop some folders here to add Originals and Scans folders" buttons {"Aye Aye"} default button "Aye Aye"
end run

Mac Automator App: How to Set Folder

This is probably something simple but its driving me crazy...
I was just able to build my first automator workflow, which is quite basic and uses the Subtitles application to download subtitles for movies in a specific folder.
The Actions are:
Get Folder Contents
Filter Finder Items (to only movie files)
Open finder item with the Subtitle app
It works fine when I execute the workflow from within Automator because it has a "Folder Action receives files and folders added to:" clause in the beginning which tells which folder to use.
However, I want to save that as an application so I can schedule that on iCal but, when I save as an app, the clause which tells the folder is no longer available and looks like I need to pass the folder name as a parameter/argument and I have no idea on how to do that.
So, what I need guidance is on how to have the app executed in iCal while telling it which folder to use.
Thanks in advance...
Just add a "run applescript" action above your "Get Folder Contents" action. Make the applescript code look like the following. Make sure to put your folder path between the quotes.
on run {input, parameters}
return "/Users/username/Desktop/"
end run
Note: a quick way to get a path is to run this in Script Editor and use the result.
return POSIX path of (choose folder)

moving one folder to another folder - applescript

I'm trying to move a user-selected folder to another folder, but I don't get it: whenever I look it up, it looks too complicated, even though I know it's not supposed to be.
Here's the code I have so far - please help:
choose folder with prompt "Select folder:"
on open of finderObject
tell application "Finder" to move (finderObject) to folder "Library:Application Support"
end open
end
First off, you have to assign the chosen folder to a variable.
Also, many special folders in the OSX file structure have special names to get their paths, including the library and the application support folders. So, your script can simply be:
set finderObject to choose folder with prompt "Select folder:"
tell app "Finder" to move finderObject to folder (path to Application Support folder from local domain as string)
However, that is the root level Library folder. I suspect you will want to use the Application Support folder in the ~/ user domain. For that, change the "from local domain" to "from user domain".

How to run script upon opening folder?

I want to run an applescript when a specific folder of mine is opened. Then, depending on the input, close the folder, or leave it open. All that without opening the folder. So, basically:
1) Try to open folder
2) Folder doesn't open, but window pops up
3-A) If user clicks ok folder opens
3-B) If user clicks cancel, it just exits out of script, leaving the folder unopened.
How can I do this? Remember: The folder CAN'T open in the background, it can ONLY open if the user presses OK. Help?
For this task, you'll want to use Folder Actions, which trigger script files. Below are the events involving a folder that could trigger a script, and the special corresponding handlers.
A folder is opened — on opening folder window for this_folder
A folder is closed — on closing folder window for this_folder
One or more items are added to a folder on adding folder items to this_folder after receiving these_items
One or more items are removed from a folder — on removing folder items from this_folder after losing these_itmes
A folder window is moved — on moving folder window for this_folder
The first bullet is what you'll be focusing on in this case. As the bullet implies, the script is triggered only when the folder actually opens. However, you can hack your way around this issue simply by adding this line at the very beginning of your script:
tell application "Finder" to close first window whose target is this_folder
And here is the full script:
on opening folder window for this_folder
tell application "Finder"
close first window whose target is this_folder
-- the window may appear briefly, but at least you've accomplished your goal
display dialog "Open folder " quoted form of the name of this_folder & "?" buttons{"Yes,"No"} default button 1 cancel button 2
-- pressing the "No" button is exactly the same as pressing the "Cancel" button on a regular dialog, and the script terminates
make new Finder window with properties {target:this_folder}
end tell
end opening folder window
SAVE YOUR SCRIPT FILE IN THE FOLDER ACTIONS FOLDER OF YOUR LOCAL SCRIPTS FOLDER. Create the folder yourself if it doesn't already exist. Doing so will allow your script to even run. Now, for this to function properly, you will need to attach the saved script file—not an application/application bundle—to your desired folder. To do this...
Perform a right-click on your desired folder
Click the menu item "Folder Actions Setup" at the bottom
Locate your saved script in the dialog that appears
Click "Attach"
Close the "Folder Actions Setup" window
Once you've done this, your script is ready to go.
Addendum: If your folder contains confidential information, beware that, as stated by user57368, "it is probably not possible to make the system even remotely secure with AppleScript."
Instead of an ordinary folder, you want an application bundle containing a folder or disk image. If you use an encrypted disk image, it will be more difficult for the user to access the contents without following your preferred procedure, but beware that it is probably not possible to make the system even remotely secure when you're using AppleScript.
If you just want to present a license agreement or other static notice before allowing the user to read the data, just read the hdiutil man page for how to create a disk image that presents a license agreement upon mounting.

Resources