Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to duplicate & rename 1 file in a folder

Hi, everyone.


I'm hoping that one of the many experienced AppleScripters in this forum will help me.


Here's what I want to do. It should be easy, but I'm just learning AppleScript, and I cannot get this to work.


I have a folder with images (all jpegs, if that matters). I want to:


1. Select a random image

tell application "Finder"

set randomImage to some file of folder "PhotoCollections"

end tell



2. Duplicate that randomImage and rename it something such as "selectedPhoto.jpg"

(the script would need to delete "selectedPhoto.jpg" first, I assume; otherwise, we would get an error)



That's it! I've tried everything I can think of for step 2, but I just get errors.


Can some of you please help? Thanks very much!


David

iMac (27-inch Mid 2011), OS X Mountain Lion

Posted on Apr 30, 2013 8:37 PM

Reply
Question marked as Best reply

Posted on Apr 30, 2013 8:41 PM

Here:


tell application "Finder"

set randomImage to some file of folder "PhotoCollections"

try

delete item "selectedPhoto.jpg" of folder "PhotoCollections"

end try

set the name of (duplicate randomImage) to "selectedPhoto.jpg"

end tell


(81932)

5 replies

Apr 30, 2013 8:54 PM in response to Niel

Niel,


Thank you so much! I've been working on this for several days and couldn't figure it out. I just tried it, and it works great!


One slight addition:


Is there a way that I can add a small time delay just before the "end tell" statement?


The reason is that it is taking 1-2 seconds for the script to delete-choose-rename the file, and I need to make sure that the process has finished before I do something else with the file.


Thank you again! I appreciate your fast help! 🙂


David

May 1, 2013 10:44 PM in response to Niel

Niel (or anyone else willing to help me!),


I wonder if you could help me again. I'm doing a similar thing as above, but this time, I need to:


1. select a random photo (or file) from a folder

2. put it into an *already open* email at the bottom as an attachment


-----


I found the following script online, and it works great. However, it creates a NEW email message and fills in fields. For my use, I need to remove most of the lines so that it doesn't create a new email but instead just uses the email window that is already open. I don't need it to fill in any of the fields. I need it only to attach the photo at the end.


I've tried taking out most of the lines, but I can't get it to work. Can you please help?


Thanks very much! 🙂


David


-----


tell application "Finder"

--- set the exact path of your folder of files (could be photos, pdfs, a mixture of types)

set random_file to some file of folder "MyMac:Users:david:Desktop:PhotoCollection"

end tell


--- change the following variables to anything you want

set theSubject to "new subject"

set theBody to "this is the message body"

set theSender to "Joe"

set theName to "Bill"

set theAddress to "email@domain.com"

set theAttachment to random_file


tell application "Mail"

set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}

tell newMessage

set visible to true

set sender to theSender

make new to recipient at end of to recipients with properties {name:theName, address:theAddress}

tell content

make new attachment with properties {file name:random_file as alias} at after the last paragraph

end tell

end tell

activate


--- uncomment the next line if you want it to automatically send this email!

--- send newMessage


end tell


-----

Script to duplicate & rename 1 file in a folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.