NooB needs help before he (I) throw my iBook across the room

I have been working on my first AS for 2 weeks now constantly using as.net as reference to my new interest, however I can’t get passed well, a couple things…

first things first:
I am creating a dropplet so when I drag either 1 or more files onto it, it seperates a 10 digit code built into the filename (12 if you count the “-”), regognizing the location of the code with a reference to the end of the filename (code behind the 10 digits) then asks for 3 locations, (which are going to be static locations after completion). It then drops the files into 1 folder with the 10 digit code sa the folder name (we will call this folderA) in the first location chosen, and folderB into the second location, as well as folderC in the third…

Now folder A and B are back-up folders for another use and are complete, however, folderA is a different story. I need to get it so the script will recongize a path such as:

“…:123:123-4:123-45:…” and so on with the original filename being “123-456-7890-2x15k.eps”

then I need it to also check if the file already exists where it then adds " - New" before the file extension.

I am going to post the code in shorter version (few references to filetypes).
Please help in any way possible. Thank you.


on open theFiles
	set theItems to every item of theFiles
	set theCount to the count of items in theItems
	repeat with i from 1 to the theCount
		set theFile to item i of theFiles
		set defaultName to theFile as text
		set type1 to "2x15k"
		--119 other types
		if defaultName contains type 1then
		set fileName to (text -12 thru -23 of defaultName)
		else
		--119 other if's and end if's
		end if
		display dialog "Please Verify the Account Number:" default answer fileName
		set folderName to fileName
		set newFile to {fileName, " New"} as text
		set directoryName to {"Choose the file directory for ", folderName} as text
		set ypgBackup to {"Choose location for the YPG Back-up"} as text
		set yourBackup to {"Choose location for You Back-up"} as text
		set thisMonth1 to {get month of (current date)} as text
		set shortMonth to (text 1 thru 3 of thisMonth1)
		set todaysDate to {shortMonth, " ", get day of (current date)} as text
		set backUp to {todaysDate, " - Mp"} as text
		set folder1a to (choose folder with prompt directoryName)
		set folder2a to (choose folder with prompt ypgBackup)
		set folder3a to (choose folder with prompt yourBackup)
-- this is where I am having the problem
		set directoryPathname1 to {text -10 thru -12 of folderName}
		display dialog directoryPathname1
		set directoryPathname2 to (text -8 thru -12 of fileName)
		set directoryPathname3 to (text -7 thru -12 of fileName)
		set directoryPathname4 to (text -6 thru -12 of fileName)
		set directoryPath1 to {directoryPathname1} of folder1a
		display dialog directoryPath1
		set directoryPath2 to directoryPathname2 of directoryPathname1
		set directoryPath3 to directoryPathname3 of directoryPathname2
		set directoryPath4 to directoryPathname4 of directoryPathname3
		if not (exists directoryPath4) then
		set folder1aa to directoryPath3
		else
		set folder1aa to directoryPath4
		end if
		tell application "Finder"
			if not (exists folder folderName of folder1aa) then
				set folder1b to (make new folder with properties {name:folderName} at folder1aa)
			else
				set folder1b to folder folderName of folder1aa
			end if
			if not (exists folder todaysDate of folder2a) then
				set folder2b to (make new folder with properties {name:todaysDate} at folder2a)
			else
				set folder2b to folder todaysDate of folder2a
			end if
			if not (exists folder backUp of folder3a) then
				set folder3b to (make new folder with properties {name:backUp} at folder3a)
			else
				set folder3b to folder backUp of folder3a
			end if
		end tell
		tell application "Finder"
-- Here is the other problem
			if not (exists theFiles of folder1b) then
			duplicate theFile to folder1b
			else
			set folder1bb to (make new folder with properties {name:newFile} at folder1b)
			duplicate the File to folder1bb
			end if
			duplicate theFile to folder2b
			duplicate theFile to folder3b
		end tell
	end repeat
end open

I have found the solution to the first problem by getting rid of the “directoryPath” and just calling the directoyPathname# a folder in the if … else commands that follow… it now reads:


if (exists folder directoryPathname4 of folder directoryPathname3 of folder directoryPathname2 of folder directoryPathname1 of folder1a) then
				set folder1b to folder directoryPathname4 of folder directoryPathname3 of folder directoryPathname2 of folder directoryPathname1 of folder1a
			else
				if (exists folder directoryPathname3 of folder directoryPathname2 of folder directoryPathname1 of folder1a) then
					set folder1b to folder directoryPathname3 of folder directoryPathname2 of folder directoryPathname1 of folder1a
				else
					if (exists folder directoryPathname2 of folder directoryPathname1 of folder1a) then
						set folder1b to folder directoryPathname2 of folder directoryPathname1 of folder1a
					end if
				end if
			end if

I still can’t get the whole file duplication so add " - New" to the name part. I am thinking it is something like:


if (exists theFile of folder1b) then
				set newfolder to (make new folder with properties {name:newFile} at folder1b)
				duplicate theFile to folder newfolder of folder1b
			else
				duplicate the file to folder1b
			end if

but that doesn’t work either…

Please… if there is anybody that has even ideas… any brainstorming would be an asset.

Thanks.

Yar!

getting… close to… 4am… can’t go to sleep with only one problem… left…

anybody have ANY ideas? I have tried everything I can think of but it just won’t work…

if the file exist in the specified folder, create a new folder with the text " - New" at the end of it. (if changing name of file, would prefer: “123-456-7890-new.eps”)

Your posts are nearly incomprehensible. Try this: What is the name of the start file? What does the final output look like? In other words, what are the final folder names, structures, and files that are derived from that initial file? e.g.:

Start file:

123-456-789.txt

Output;

123:MMDDYY:file.txt
456:MMDDYY:file.txt
789:MMDDYY:file.txt

You are making your code far too complicated. For instance, why assign variables for the prompts to the choose folder commands when you can just use strings in the commands themselves? No variables are needed for this and, if you must use variables because you want to localize it later, why are you using single item lists instead of strings? This type of thing makes your code very difficult to parse and is probably why you have not received any replies–just looking at makes my eyes cross. Be very concise with what you have and what you need to accomplish then see if you can clean your code to make it more palatable and we’ll go from there.

Jon

input:

file: 123-456-7890-2udkb.eps

output:
“123:123-4:123-45:123-456-7890-2udkb.eps”
(if the file doesn’t already exhist)

“123:123-4:123-45:123-456-7890-New:123-456-7890-2udkb.eps”
or
“123:123-4:123-45:123-456-7890-2udkb-new.eps”
(if the file does previously exist)

The code is basically having problems checking if the file exists. if there is a way to say:


if (exists item the_File in the_Folder) then
set new_folder to (make new folder with properties {name:abrvFolder} at the_folder
-- abrvFolder has been defined
duplicate the_File to new_folder
else
duplicate the_File to the_Folder
end if

Download the script and a dummy file and folder here.
Click to Download

complete!! I changed the if statment to a try with an error alternative:


			try
				duplicate theFile to folder1b
			on error
				set newfolder to (make new folder with properties {name:newFile} at folder1b)
				duplicate theFile to newfolder
			end try

thanks for trying, I do appreciate it. I can already see things I have to add to this code so I may be back yet.

Glad you solved it. I didn’t follow the logic of the how you were extracting the folder names from the file name but I’m happy you seem to have got things working now. As I mentioned before, really try to streamline your code since you were make lots of unnecessary variable assignments and assigning value types to variables that were the wrong type that you just needed to coerce immediately for no reason when you could have set them properly in the first place. As an example, I’ve cleaned up the some of the beginning of the original script you posted:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]