name change and upload generates error message

Here is a script I am working on. What it does (supposed to do) is make sure each file dropped on it is a jpg, then changes each file’s name to today’s date (with an additional append to differentiate between the files), then upload it to a remote ftp server.

When I drop a file on it, it generates a stack error

This script is a compilation of several scripts and so there’s no telling what I’ve screwed up.

Any help is appreciated.

Don

Here is the full script (with certain personal items deleted)

on run
	display dialog "This is a droplet, so drop some \"jpg\" files on me!" buttons {"OK"} default button 1 with icon 2
end run

on open input_items
	
	
	set ftpSite to "enter ftp server and path here"
	set ftpUser to "user name"
	set ftpPass to "password"
	set URLprefix to ""
	
	-- STOP HERE -- Don't Change Anything Else
	
	-- this is where we pluck today's date to use as a file name
	set the iDay to day of the (current date) as integer
	set the iMonth to month of the (current date) as integer
	set the iYear to year of the (current date) as integer
	
	--this handles each file of multiple files dropped on the droplet
	repeat with i from 1 to number of items of input_items
		set thisItem to item i of input_items
		set {name:Nm, name extension:Ex} to info for (thisItem as alias)
		
		if Ex is in {"jpg", "jpeg"} then
			
			-- here we create the name of the file
			set photoFileName to (iDay as string) & (iMonth as string) & (iYear as string) & (i as string) & ".jpg"
			-- here is where we rename the file
                        -- AND I THINK THIS IS WHERE THE ERROR OCCURS
                        set the name of thisItem to photoFileName
			-- here we pass info to the upload routine below
			set done to simpleFtpUpload(ftpSite, this_item, ftpUser, ftpPass)
			if URLprefix is not "" then
				set the clipboard to URLprefix & item_name
			end if
			if done > 1 then
				display dialog item_name & " has been uploaded." buttons {"OK"} default button 1 giving up after 5
			end if
		else
			display dialog item_name & " is not a jpg file and cannot be uploaded." buttons {"OK"} default button 1
			--end if
		end if
		
		
	end repeat
	
	return input_items
end open

-- The simpleFtpUpload subroutine
on simpleFtpUpload(remoteURL, localFile, userName, userPasswd)
	-- version 1.2, Dan Shockley (http://www.danshockley.com)
	-- uses curl to do the upload
	-- remoteURL is the complete ftp url to the remote destination directory
	try
		set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
		set parentFolder to (text items 1 thru -2 of (localFile as string)) as string
		set AppleScript's text item delimiters to od
		
		set localFile to localFile as string
		
		set localPosixPath to quoted form of POSIX path of localFile
		set uploadFileName to do shell script "basename " & localPosixPath
		set uploadDirectory to quoted form of POSIX path of parentFolder
		
		-- curl --upload-file SOMEFILE ftp://SERVER.com --user MYUSER:THEPASSWD
		-- must be IN the directory of the file you want to upload
		set myCommand to "cd " & uploadDirectory & ";" & "curl --disable-epsv --upload-file "
		set myCommand to myCommand & quoted form of uploadFileName
		set myCommand to myCommand & " " & remoteURL
		set myCommand to myCommand & " --user " & userName & ":" & userPasswd
		set myCommand to myCommand & " " & "--write-out " & "%{size_upload}"
		--set myCommand to myCommand & " --quote -quit"
		
		-- output is the 'size_upload' result from curl
		
		set uploadResult to do shell script myCommand
		
		
		return uploadResult -- size uploaded in kilobytes
	on error errmsg number errNum
		display dialog "simpleFtpUpload FAILED: " & errmsg
		-- error "simpleFtpUpload FAILED: " & errmsg number errNum
	end try
end simpleFtpUpload

Hi,

only the Finder can rename a file.
Here is your open handler with an easier date formatting method


on open input_items
	
	set ftpSite to "enter ftp server and path here"
	set ftpUser to "user name"
	set ftpPass to "password"
	set URLprefix to ""
	
	-- STOP HERE -- Don't Change Anything Else
	
	--this handles each file of multiple files dropped on the droplet
	repeat with i from 1 to number of items of input_items
		set thisItem to item i of input_items
		if name extension of (info for thisItem) is in {"jpg", "jpeg"} then
			
			-- here we create the name of the file
			set photoFileName to do shell script "/bin/date +%d%m%Y" & i & ".jpg"
			-- here is where we rename the file
			tell application "Finder" to set the name of thisItem to photoFileName
			-- here we pass info to the upload routine below
			set done to simpleFtpUpload(ftpSite, this_item, ftpUser, ftpPass)
			if URLprefix is not "" then
				set the clipboard to URLprefix & item_name
			end if
			if done > 1 then
				display dialog item_name & " has been uploaded." buttons {"OK"} default button 1 giving up after 5
			end if
		else
			display dialog item_name & " is not a jpg file and cannot be uploaded." buttons {"OK"} default button 1
			--end if
		end if
	end repeat
	
	return input_items
end open

Thanks Stefan. With a little tweaking, what you sent was marvelous. Does exactly what I wanted.

Because these files are being uploaded to a Cold Fusion (Windoze) server, they all need to have distinct names, so I look in the man for date and added the seconds in the shell script, creating a long file name, but each should be distinct.

Here’s the whole script:


on run
	display dialog "This is a droplet, so drop some \"jpg\" files on me!" buttons {"OK"} default button 1 with icon 2
end run

on open input_items
	
	
	set ftpSite to "the URL of your ftp target site"
	set ftpUser to "user name"
	set ftpPass to "password"
	set URLprefix to ""
	
	-- STOP HERE -- Don't Change Anything Else
	
	--this handles each file of multiple files dropped on the droplet
	repeat with i from 1 to number of items of input_items
		--select the item to upload, one at a time
		set thisItem to item i of input_items
		--Make sure the file is a jpg
		if name extension of (info for thisItem) is in {"jpg", "jpeg"} then
			
			-- here we create the name of the file
			set photoFileName to do shell script "/bin/date +%d%m%Y%s" & ".jpg"
			--These are the leftover arguments from the shell script. 
			-- here is where we rename the file
			tell application "Finder" to set the name of thisItem to photoFileName
			-- here we pass info to the upload subroutine below
			set done to simpleFtpUpload(ftpSite, thisItem, ftpUser, ftpPass)
			if URLprefix is not "" then
				set the clipboard to URLprefix & photoFileName
			end if
			--notify the user that the file has been uploaded
			if done > 1 then
				display dialog photoFileName & " has been uploaded." buttons {"OK"} default button 1 giving up after 5
			end if
		else
			--if the file is not a jpg, tell the user so
			display dialog item_name & " is not a jpg file and cannot be uploaded." buttons {"OK"} default button 1
			--end if
		end if
	end repeat
	
	return input_items
end open

-- The simpleFtpUpload subroutine
on simpleFtpUpload(remoteURL, localFile, userName, userPasswd)
	-- version 1.2, Dan Shockley (http://www.danshockley.com)
	-- uses curl to do the upload
	-- remoteURL is the complete ftp url to the remote destination directory
	try
		set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
		set parentFolder to (text items 1 thru -2 of (localFile as string)) as string
		set AppleScript's text item delimiters to od
		
		set localFile to localFile as string
		
		set localPosixPath to quoted form of POSIX path of localFile
		set uploadFileName to do shell script "basename " & localPosixPath
		set uploadDirectory to quoted form of POSIX path of parentFolder
		
		-- curl --upload-file SOMEFILE ftp://SERVER.com --user MYUSER:THEPASSWD
		-- must be IN the directory of the file you want to upload
		set myCommand to "cd " & uploadDirectory & ";" & "curl --disable-epsv --upload-file "
		set myCommand to myCommand & quoted form of uploadFileName
		set myCommand to myCommand & " " & remoteURL
		set myCommand to myCommand & " --user " & userName & ":" & userPasswd
		set myCommand to myCommand & " " & "--write-out " & "%{size_upload}"
		--set myCommand to myCommand & " --quote -quit"
		
		-- output is the 'size_upload' result from curl
		
		set uploadResult to do shell script myCommand
		
		
		return uploadResult -- size uploaded in kilobytes
	on error errmsg number errNum
		display dialog "simpleFtpUpload FAILED: " & errmsg
		-- error "simpleFtpUpload FAILED: " & errmsg number errNum
	end try
end simpleFtpUpload