Combining 3 scripts into 1

Currently I have 3 scripts that I would like to combine into 1. A little backstory: Everyday I receive many files formatted like this: #1photo.jpg, #2photo.jpg, etc. I need them to sort into folders that have been previously created based on their numbers, i.e: #1photo.jpg would need to be moved into a folder titled 1_Sometext, #2photo.jpg would need to be moved to folder titled 2_Sometext, and so on. Right now I have 3 separate codes that seem to accomplish what I need but I was hoping to either combine them or streamline the process/code somehow. The first code removes the hashtag from the filename. The second code removes all the letters from the filename. The third code moves the files into the numbered folders.

First Code: Removing Hashtags


set whichFile to choose file with multiple selections allowed
repeat with aFile in whichFile
	tell application "Finder"
		set filename to name of aFile
		set name of aFile to ((characters 2 thru -1 of filename) as string)
	end tell
end repeat

Second Code: Removing Letters from Filename

set theF to choose file
tell application "System Events"
	tell item (theF as text)
		set {name:fileName, name extension:fileExtension} to it
		set parentPath to path of its container as text
	end tell
end tell
try
	set trimmedFileName to (do shell script "tr -d '[:alpha:]' <<<" & quoted form of fileName & " | grep -E [[:digit:]] ")
on error
	-- no digits in the resulting filename!
	set trimmedFileName to ""
end try
if trimmedFileName is not "" then
	tell application "System Events"
		if not (exists item (parentPath & trimmedFileName & fileExtension)) then
			tell item (theF as text)
				set name of it to (trimmedFileName & fileExtension)
			end tell
		end if
	end tell
end if

Third Code: Moving Files into Folders Based on Matching Numbers


set baseFolder to choose folder with prompt "Choose Folder with Photos"
set printFolder to choose folder with prompt "Choose Today's Versioning Folder"
-- or hard-coded
-- set printFolder to alias "MacHD:Users:myuser:path:to:Print:"
tell application "Finder" to set fileList to files of baseFolder
repeat with aFile in fileList
	set prefix to getPrefix(name of aFile, name extension of aFile)
	tell application "Finder"
		try
			set destinationFolder to (1st folder of printFolder whose name begins with (prefix & "_"))
			move aFile to destinationFolder
		end try
	end tell
end repeat

on getPrefix(aName, anExtension)
	set {TID, text item delimiters} to {text item delimiters, "_"}
	if (count text items of aName) > 1 then
		set prefix to text item 1 of aName
	else
		set prefix to text 1 thru ((get offset of "." & anExtension in aName) - 1) of aName
	end if
	set text item delimiters to TID
	return prefix
end getPrefix

I am very new to all this but trying to pick it up as I go along. Any and all help is greatly appreciated. Thanks!!

PS - I am using OS X 10.9.5 and AppleScript Editor 2.3.2

I’m not sure to get everything but try this, it’s more efficient to use a single routine for code #1 and #2 (getPrefix).
As you need only the digits at the beginning of the filename, the getPrefix handler is left when the first non-digit character is detected


set baseFolder to choose folder with prompt "Choose Folder with Photos"
set printFolder to choose folder with prompt "Choose Today's Versioning Folder"
-- or hard-coded
-- set printFolder to alias "MacHD:Users:myuser:path:to:Print:"
tell application "Finder" to set fileList to files of baseFolder
repeat with aFile in fileList
	set prefix to getPrefix(aFile)
	tell application "Finder"
		try
			set destinationFolder to (1st folder of printFolder whose name begins with (prefix & "_"))
			move aFile to destinationFolder
		end try
	end tell
end repeat

on getPrefix(theFile)
	tell application "Finder" to set filename to name of theFile
	set prefix to ""
	set flag to false
	repeat with i from 1 to (length of filename)
		if (get offset of (item i of filename) in "0123456789#") is not 0 then
			set flag to true
			set prefix to prefix & item i of filename
		else
			if flag is true then exit repeat
		end if
	end repeat
	return prefix
end getPrefix


Thanks StefanK! I ran the script and no errors were returned but also nothing happened. The file names didn’t change and the files weren’t moved.

Thanks again for the reply!

sorry, I misinterpreted the treatment of the # character.

Replace

if (get offset of (item i of filename) in "0123456789#") is not 0 then

with

if item i of filename is not "#" and (get offset of (item i of filename) in "0123456789") is not 0 then

Thank you!!! That worked like a charm!!