Moving Recently Created Files

Hello everyone, I’m very new to Applescripts and this was to be my first script – mostly hacked together from other scripts I found, but I can’t seem to get it work completely. Ideally what it’s supposed to do is take the selected file and then 1. duplicate it, 2. rename the duplicate, and then 3. move the duplicate to a specified folder. I’ve been able to get the script to do 1 and 2, and then 3, but not consecutively.

tell application "Finder" to repeat with theItem in (get selection)
	activate
	set currName to theItem's name
	set (duplicate theItem)'s name to "Aqua Blue.jpg"
	
	if (exists file "Aqua Blue.jpg") = true then
		move "Aqua Blue.jpg" to folder "Desktop Pictures" of folder ¬
			"Library" of startup disk with replacing
	end if
end repeat

Any help would be greatly appreciated.

I think this is more of what you want


set destination to ("" & (path to current user folder) & "Library:Desktop Pictures")

tell application "Finder"
	repeat with theItem in (get selection)
		duplicate (theItem as alias) to folder destination with replacing
	end repeat
end tell

I’m almost posative that you can rename it while your duplicating it but I can’t seem to remember how to do that.

mm

Hi,

the easiest way to duplicate and rename at the same time is to use the shell cp command
I’m sorry, I don’t understand the line

if (exists file "Aqua Blue.jpg")

this checks if the file Aqua Blue.jpg exists on desktop, nowhere else

tell application "Finder" to repeat with theItem in (get selection)
    if (exists file "Aqua Blue.jpg") then
        do shell script "cp " & quoted form of POSIX path of (theItem as alias) & space & "/Library/Desktop\\ Pictures/Aqua\\ Blue.jpg"
    end if
end repeat

Thank you for help.

However, I don’t fully understand the shell script, I tried running it and it didn’t seem to output any results. Not to knock shell commands, but is it possible to do it with just regular commands? I was thinking perhaps this series of actions might make the script work – 1. get file name 2. rename it Aqua Blue.jpg 3. duplicate it to /Library/Desktop Pictures 4. rename the original file back to its original name.

I tried this, but it returned an error I couldn’t decipher:

set destination to ("" & (path to startup disk) & "Library:Desktop Pictures")


tell application "Finder"
	repeat with theItem in (get selection)
		set currName to theItem's name
		set (theItem)'s name to "Aqua Blue.jpg"
		duplicate (theItem as alias) to folder destination with replacing
		
		set (theItem)'s name to currName
	end repeat
end tell

I found that with these kind of file actions, shell script is the easiest and better way to go. I would really recommend for you to try to understand it. Also, I think there is an error in Stefan’s script when it asks for the existance of ‘Aqua Blue.jpg’.

You may try this adaptation of Stefan’s script:

set destinationPath to POSIX path of (path to current user folder) & "Library/Desktop Pictures/"
set theNewFile to destinationPath & "Aqua Blue.jpg"

-- or, in one line: 
-- set theNewFile to POSIX path of (path to current user folder) & "Library/Desktop Pictures/Aqua Blue.jpg"

tell application "Finder" to repeat with theItem in (get selection)
	do shell script "cp " & quoted form of POSIX path of (theItem as alias) & space & quoted form of theNewFile
end repeat

What the shell script does is copy the selected file to the new destination under the new name.

What I’m unclear about is your use of ‘get selection’… How are you making the selection? Would ‘choose file’ not be what you want?

set destinationPath to POSIX path of (path to current user folder) & "Library/Desktop Pictures/"
set theNewFile to destinationPath & "Aqua Blue.jpg"

set theItem to choose file
do shell script "cp " & quoted form of POSIX path of (theItem as alias) & space & quoted form of theNewFile

Stadsman, did a little tweaking and it worked perfectly. Thanks for everyone’s help, it is greatly appreciated.

Stadsman,

 I too was confused by this command because I always use choose file or choose folder and cycle though the items in it. But get selection actually picks what you have selected in the finder.
 This has an advantage...you can just select the file(s) you want the script to process  without having to sift through the files in the script(choose folder) or do them one at a time(choose file) or navigate to the file or folder to select. 
 Another alternative would be to make it a droplet... the difference there being that the file(s) are drop onto the script there by lanching the script and processing the files

this can be done like this…


set newFile to quoted form of POSIX path of (path to current user folder) & "Library:Desktop Pictures:Aqua Blue.jpg"

on open theFiles
	
	repeat with afile in theFiles
		set afile to quoted form of POSIX path of afile
		do shell script "cp " & afile & space & newFile
	end repeat
	
end open

of course the problem I see here is that if you drop 10 files on the script you will still end up with only one “Aqua Blue.jpg” and there is no validation to ensure that you copying a JPG

mm

Thanks for the input Mcgrailm. While checking to make sure that the file is a JPG would be awesome, I don’t think it’s completely necessary as the people I believe are smart enough to know what files to use this with. However, I did manage to get an error saying that the script doesn’t have permission to replace that image in Library/Desktop Pictures… I think this happens when the User is not an administrator. Is it possible to have the script pop-up that dialog that would let the user validate the process (ie. put in the admin name and password) so it can copy the image over to the system folder?

BTW, this is the script

	set theNewFile to POSIX path of (path to startup disk) & "Library/Desktop Pictures/Aqua Blue.jpg"
	
	tell application "Finder" to repeat with theItem in (get selection)
		do shell script "cp " & quoted form of POSIX path of (theItem as alias) & space & quoted form of theNewFile
	end repeat

it may be best to just allow everyone to access that folder rather than giving people the adim user name and password ? I’m sure there is a way to do what your asking I just don’t know how to do that myself …

MM

Well I’m releasing this script into the web, so I wouldn’t be personally giving my admin user/pass around, but I know that some people don’t run as admin, but would still like to be able to run the script.

I tried this

property runasroot : true

on superstart()
	set theNewFile to POSIX path of (path to startup disk) & "Library/Desktop Pictures/Aqua Blue.jpg"
	
	tell application "Finder" to repeat with theItem in (get selection)
		do shell script "cp " & quoted form of POSIX path of (theItem as alias) & space & quoted form of theNewFile
	end repeat
	do shell script "echo Shell script executed." administrator privileges runasroot
end superstart

superstart()

It executes properly under the admin user, but under a regular user it still gives an error without allowing them to authenticate.

This script will ask for admin password and quits when a false password is entered or when cancelled:

set theNewFile to POSIX path of (path to startup disk) & "Library/Desktop Pictures/Aqua Blue.jpg"

try
	do shell script "echo ''" with administrator privileges
	tell application "Finder" to repeat with theItem in (get selection)
		do shell script "cp " & quoted form of POSIX path of (theItem as alias) & space & quoted form of theNewFile
	end repeat
on error
	quit
end try

Hi,

some notes:

¢ there is a “shortcut” to the Desktop picture folder (path to desktop pictures folder)
¢ the repeat loop is useless, because all items of the Finder selection will be copied to the same destination file.
I recommend to take only the first item of the selection (the error if nothing is selected has to be trapped)
¢ only using with administrator privileges is also useless in an admin account, unless using a shell script with
the sudo command. To use it in a standard account,
the syntax must be user name “username” password “password” with administrator privileges, user name and
password must be the data of an admin account. (but this works only in OS 10.4 and higher)

Here a version with a file check for jpg (but no password validation)

on superstart()
	set theNewFile to quoted form of POSIX path of ((path to desktop pictures folder as Unicode text) & "Aqua Blue.jpg")
	try
		tell application "Finder" to set theItem to (item 1 of (get selection) as alias)
		tell (info for theItem) to if name extension is "jpg" or kind contains "JPEG" or kind contains "JPG" then
			do shell script "cp " & quoted form of POSIX path of theItem & space & theNewFile
		end if
	end try
end superstart

Awesome, you read my mind. After a bit of playing around I had decided that Mcgrailm was on the right track suggesting error checking for JPG. However, when I tried your variant of the script, it didn’t do anything. Also is it possible to throw in an error dialog stating that this isn’t a JPG file?

Also, are you saying if I include

it will ask for authentication if the user is not the admin? Because if that’s the case, it would be fine if it only worked for 10.4, but that should also output an error if it couldn’t be copied properly.

Thank you guys so much for your help. I think I might venture out today and pick up a book on AppleScripting :slight_smile:

Sorry, my fault.
In my version the do shell script is within the tell block and doesn’t work

on superstart()
	set theNewFile to quoted form of POSIX path of ((path to desktop pictures folder as Unicode text) & "Aqua Blue.jpg")
	try
		tell application "Finder" to set theItem to (item 1 of (get selection) as alias)
		tell (info for theItem) to name extension is "jpg" or kind contains "JPEG" or kind contains "JPG"
		if result then
			do shell script "cp " & quoted form of POSIX path of theItem & space & theNewFile
		else
			display dialog "The chosen file is not JPG image" buttons {"Cancel"} default button 1
		end if
	end try
end superstart

superstart()

I’m afraid I don’t know this exactly. A big part of developing AppleScripts is trial & error :wink:

Thanks for help, in the end my code looks like this:


on superstart()
	set theNewFile to quoted form of POSIX path of ((path to desktop pictures folder as Unicode text) & "Aqua Blue.jpg")
	
	try
		tell application "Finder" to set theItem to (item 1 of (get selection) as alias)
		tell (info for theItem) to name extension is "jpg" or kind contains "JPEG" or kind contains "JPG"
		if result then
			do shell script "cp " & quoted form of POSIX path of theItem & space & theNewFile with administrator privileges
		else
			display dialog "The selected file is not a JPG image" buttons {"OK"} default button 1
		end if
	end try
end superstart

superstart()

It seems to run fine as both admin and user on 10.4.9 (though if anyone else wants to verify it for me, that would be great!

I did find this little snippet from an Apple mailing listhttp://lists.apple.com/archives/Applescript-users/2005/Jan/msg00719.html

set admin to do shell script "whoami | id | grep admin"

if admin ≠ "" then -- not equal to
	set accountType to "admin"
else
	set accountType to "admin"
end if

Somehow I would imagine that with this code it would to me to only have to authenticate if the person running the script is not running under an Admin account, but I’m not really sure what the script is doing since it states "set accountType to ‘admin’ twice. "

In either case, thanks a heap to everyone who threw there two cents in, it has been much appreciated!

this is indeed useless :slight_smile:
Also whoami is not needed, because id without an argument displays the
information about the current user

this sets the variable to a boolean value (true or false)

set isAdmin to (do shell script "id | grep admin") is not ""

Thanks for the input. Well seeing as I’m no Applescript Ninja and the script already does what I need it to do (thanks to you guys), I’ll just leave it as is. So, another case closed, until next time.

Thanks again, everyone!