a simple sorting script - beware high levels of noobness

Hello

My first post here, MacScripter seems to be a very helpful and pleasant place! I hope to get more assistance than I did over at the Apple forums - they were useless! Anyways, down to business.

Quite simply, I want a ‘Sorting Bin’ on my Desktop, which will do various things with any files/folders dropped on it, such as:

  1. categorise different file types into respective folders (in sensible groupings, so PDFs and text files in ‘Documents’)
  2. also, to move certain specific types to other places, such as pictures to the Pictures folder of my home folder.

I also want to be able to set is a Folder Action, so it is activated upon receiving new files into the Sorting Bin.

I know this is demanding of me, especially as I’m such a noob, but I’m keen to learn and I’ve done as much searching as I can for similar ideas and solutions.

I’ve found the following script which does a lot of what I want (thanks to wayfarer_boy, at the following post: http://bbs.applescript.net/viewtopic.php?id=10365 )

property imageList : {"gif", "jpg", "psd", "tif", "pic", "pict", "jpeg"}
property textList : {"txt", "doc", "rtf", "clpt", "tab", "sql"}
property pdfList : {"pdf"}
property soundsList : {"wav", "mp3", "m4a", "aif", "aiff"}
property flashList : {"fla", "swf"}
property directorList : {"dir", "dcr"}
property macList : {"sit", "dmg"}
property pcList : {"exe", "zip"}
property freehandList : {"AGD6"}
property webList : {"htm", "php"}
property finalList : {"FCPF"}
property filemakerList : {"fp5"}
property afterList : {"aep"}
property movieList : {"mov", "avi", "mpg", "MooV", "MPEG", "3gp"}
property appList : {"appl"}

property typeList : {}

on open TheFolder
	tell application "Finder"
		(* set the TheFolder to the dropped folder name *)
		set TheFolder to TheFolder as text
		(* set FolderList to the dropped folder name - in case of multiple folders, list as list *)
		set FolderList to {TheFolder}
		(* start with first item on list of dropped items *)
		(* and set TheFolder to the first dropped item *)
		set TheFolder to item 1 of FolderList
		set TheFolder to TheFolder as text
		repeat
			try
				set TheType to {name, file type, creator type} of first file of folder TheFolder
				set TheName to item 1 of TheType
				set TheFileType to item 2 of TheType
				set N to count of characters of TheName
				if (N > 3) and character (N - 3) of TheName is "." then
					set TheName to characters (N - 2) thru N of TheName
					set TheName to TheName as text
				end if
				if ((imageList contains TheName) or (imageList contains TheFileType)) then
					set TheFolderName to "Images"
				else if ((textList contains TheName) or (textList contains TheFileType)) then
					set TheFolderName to "Text files"
				else if ((pdfList contains TheName) or (pdfList contains TheFileType)) then
					set TheFolderName to "PDF files"
				else if ((soundsList contains TheName) or (soundsList contains TheFileType)) then
					set TheFolderName to "Sounds"
				else if ((flashList contains TheName) or (flashList contains TheFileType)) then
					set TheFolderName to "Flash files"
				else if ((directorList contains TheName) or (directorList contains TheFileType)) then
					set TheFolderName to "Director files"
				else if ((macList contains TheName) or (macList contains TheFileType)) then
					set TheFolderName to "Mac files"
				else if ((pcList contains TheName) or (pcList contains TheFileType)) then
					set TheFolderName to "PC files"
				else if ((freehandList contains TheName) or (freehandList contains TheFileType)) then
					set TheFolderName to "Freehand files"
				else if ((webList contains TheName) or (webList contains TheFileType)) then
					set TheFolderName to "Site files"
				else if ((finalList contains TheName) or (finalList contains TheFileType)) then
					set TheFolderName to "Final Cut files"
				else if ((filemakerList contains TheName) or (filemakerList contains TheFileType)) then
					set TheFolderName to "Filemaker files"
				else if ((afterList contains TheName) or (afterList contains TheFileType)) then
					set TheFolderName to "After Effect files"
				else if ((movieList contains TheName) or (movieList contains TheFileType)) then
					set TheFolderName to "Movies"
				else if ((appList contains TheName) or (appList contains TheFileType)) then
					set TheFolderName to "Application files"
				else
					set TheFolderName to "Unknown"
				end if
				try
					move file 1 of folder TheFolder to folder TheFolderName of folder TheFolder
				on error
					make new folder at folder TheFolder with properties {name:TheFolderName}
				end try
			on error
				exit repeat
			end try
		end repeat
	end tell
end open

Now, thanks to Applescript’s pseudo-english, this makes some sense to me. I understand how it organises each type into ‘groups’ so it can treat them separately. This enables me to fiddle with the groups and add extra types. So far, so good. I understand how to rename the respective folders also.

I’m keen to learn: if any experts fancy annotating this script with a few one-liners explaining what various bits do I’d be very grateful - this would enable me to learn by fiddling.

Things I want to add to this script -

  1. I want to add the ‘activate on receiving a file’ Folder actions trigger to it, and am unsure how.
  2. If possible, I want to be able to move certain groups elsewhere rather than into their folders-by-type, such as images to my Pictures folder in my Home folder. If anyone could add this tiny functionality and indicate which bits to fiddle with to customise it, I’d be absolutely over the moon and very grateful.
  3. Does the script need adjusting so that it would wait until larger files have finished copying before activating? Perhaps a strategically placed pause?

Cheers guys, thanks so much for helping.

Model: iMac G5 20"
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Well as for Question 1, as opposed to on open handler you want something like this…

on adding folder items to thisFolder after receiving theItems
	repeat with anItem in theItems
		-- do your stuff here
	end repeat
end adding folder items to

As for question 2, Can you clarify what it is your looking for? Perhaps I haven’t had enough coffee yet (which is very likely), but that confused me.

Thank you so much for replying. In the last few hours I’ve actually had a bash at putting the trigger in myself, setting up the folder action in a ‘Sorting Bin’ on my Desktop, and fiddled with the script to get rid of obsolete file types and add some of my own, and to make them move to a different folder - the ‘Filing Cabinet’ (for want of a better name) in my user folder.

I realised that if I had the script funnel the files into sub-folders within the folder being checked by the folder action, then it would keep triggering itself… leading to goodness knows what. So I set up the Filing Cabinet folder.

To some extent everything seems to work.

property imageList : {"gif", "jpg", "psd", "tif", "pic", "pict", "jpeg"}
property textList : {"txt", "doc", "rtf", "clpt", "tab", "sql", "xls"}
property pdfList : {"pdf"}
property soundsList : {"wav", "mp3", "m4a", "aif", "aiff"}
property macList : {"sit", "dmg", "zip", "bin", "hqx", "sea", "cpt", "arc", "z", "gz", "tar"}
property movieList : {"mov", "avi", "mpg", "MooV", "MPEG", "3gp", "wmv"}
property appList : {"appl"}
property scptList : {"scpt", "workflow"}
property internetList : {"webloc"}
property widgetList : {"wdgt"}

property typeList : {}

on adding folder items to this_folder
	tell application "Finder"
		(* set the this_folder to the dropped folder name *)
		set this_folder to this_folder as text
		(* set FolderList to the dropped folder name - in case of multiple folders, list as list *)
		set FolderList to {this_folder}
		(* start with first item on list of dropped items *)
		(* and set this_folder to the first dropped item *)
		set this_folder to item 1 of FolderList
		set this_folder to this_folder as text
		repeat
			try
				set TheType to {name, file type, creator type} of first file of folder this_folder
				set TheName to item 1 of TheType
				set TheFileType to item 2 of TheType
				set N to count of characters of TheName
				if (N > 3) and character (N - 3) of TheName is "." then
					set TheName to characters (N - 2) thru N of TheName
					set TheName to TheName as text
				end if
				if ((imageList contains TheName) or (imageList contains TheFileType)) then
					set this_folderName to "Images"
				else if ((textList contains TheName) or (textList contains TheFileType)) then
					set this_folderName to "Documents"
				else if ((pdfList contains TheName) or (pdfList contains TheFileType)) then
					set this_folderName to "PDF files"
				else if ((soundsList contains TheName) or (soundsList contains TheFileType)) then
					set this_folderName to "Music & Sounds"
				else if ((macList contains TheName) or (macList contains TheFileType)) then
					set this_folderName to "Disk Images & Archives"
				else if ((movieList contains TheName) or (movieList contains TheFileType)) then
					set this_folderName to "Movies"
				else if ((appList contains TheName) or (appList contains TheFileType)) then
					set this_folderName to "Application files"
				else if ((scptList contains TheName) or (scptList contains TheFileType)) then
					set this_folderName to "Scripts & Workflows"
				else if ((internetList contains TheName) or (internetList contains TheFileType)) then
					set this_folderName to "Internet"
				else if ((widgetList contains TheName) or (widgetList contains TheFileType)) then
					set this_folderName to "Widgets"
				else
					set this_folderName to "Other"
				end if
				try
					move file 1 of folder this_folder to folder this_folderName of folder "Filing Cabinet" of folder "Nathan" of folder "Users" of startup disk
				on error
					make new folder at folder "Filing Cabinet" of folder "Nathan" of folder "Users" of startup disk with properties {name:this_folderName}
				end try
			on error
				exit repeat
			end try
		end repeat
	end tell
end adding folder items to

However not all the file types I’ve added are being picked up, and lots are ending up being put in the ‘Other’ folder. I can’t see why this is happening. I added the scptList, internetList, and widgetList properties, and added actions for them respectively. What have I done wrong? Have I got the extensions wrong? Is it something to do with this bit…

set TheType to {name, file type, creator type} of first file of folder this_folder
				set TheName to item 1 of TheType
				set TheFileType to item 2 of TheType
				set N to count of characters of TheName
				if (N > 3) and character (N - 3) of TheName is "." then
					set TheName to characters (N - 2) thru N of TheName
					set TheName to TheName as text
				end if

…which I think is allowing the script to find the extension within long names?

Pointers would be much appreciated. Cheers!

I use this to get the base name, extension, and file type of a file:

set f to (choose file)
tell application "Finder" to tell (info for f) to set {Nm, Ex, FT} to {name, name extension, file type}
set BN to text items 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm as string
{BN, Ex, FT}

By choosing examples of files with the extensions you care about you can examine the file types as well. They aren’t always like the extensions.

EDIT: Nigel Garvey has pointed out to me in a PM that I don’t have to tell the Finder to get info for; AppleScript manages that by itself from the Standard Additions osax. Further, if “text 1 thru …” is used instead of “text items …” then the “as string” is not required. (Thanks to NG as well).

set f to (choose file)
tell (info for f) to set {Nm, Ex, FT} to {name, name extension, file type}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm 
{BN, Ex, FT}

Hi, splodgecat.

This code specifically finds extensions that have three characters. If an extension has four characters, for example, ‘TheName’ won’t be set to it. As Adam’s suggested, both the Finder and the ‘info for’ command from the StandardAdditions are capable of returning the name extension as an entity in itself.

Your main script’s a little confusing to read because the variable holding the extension is called ‘TheName’. It’s also a little inefficient in that, if it has to create a storage folder for a file, it doesn’t store the file in it immediately but goes round the repeat and tests the file again. Here’s a modified version that I hope you’ll like. Since the original didn’t make any use of the file’s creator type, or of the property ‘typeList’, this version omits them altogether.

property imageList : {"gif", "jpg", "psd", "tif", "pic", "pict", "jpeg"}
property textList : {"txt", "doc", "rtf", "clpt", "tab", "sql", "xls"}
property pdfList : {"pdf"}
property soundsList : {"wav", "mp3", "m4a", "aif", "aiff"}
property macList : {"sit", "dmg", "zip", "bin", "hqx", "sea", "cpt", "arc", "z", "gz", "tar"}
property movieList : {"mov", "avi", "mpg", "MooV", "MPEG", "3gp", "wmv"}
property appList : {"appl"}
property scptList : {"scpt", "workflow"}
property internetList : {"webloc"}
property widgetList : {"wdgt"}

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		try
			set FilingCabinet to folder "Filing Cabinet" of home
		on error
			set FilingCabinet to (make new folder at home with properties {name:"Filing Cabinet"})
		end try
		repeat with ThisItem in added_items
			try
				set {TheExtension, TheFileType} to {name extension, file type} of item ThisItem
				if ((imageList contains TheExtension) or (imageList contains TheFileType)) then
					set this_folderName to "Images"
				else if ((textList contains TheExtension) or (textList contains TheFileType)) then
					set this_folderName to "Documents"
				else if ((pdfList contains TheExtension) or (pdfList contains TheFileType)) then
					set this_folderName to "PDF files"
				else if ((soundsList contains TheExtension) or (soundsList contains TheFileType)) then
					set this_folderName to "Music & Sounds"
				else if ((macList contains TheExtension) or (macList contains TheFileType)) then
					set this_folderName to "Disk Images & Archives"
				else if ((movieList contains TheExtension) or (movieList contains TheFileType)) then
					set this_folderName to "Movies"
				else if ((appList contains TheExtension) or (appList contains TheFileType)) then
					set this_folderName to "Application files"
				else if ((scptList contains TheExtension) or (scptList contains TheFileType)) then
					set this_folderName to "Scripts & Workflows"
				else if ((internetList contains TheExtension) or (internetList contains TheFileType)) then
					set this_folderName to "Internet"
				else if ((widgetList contains TheExtension) or (widgetList contains TheFileType)) then
					set this_folderName to "Widgets"
				else
					set this_folderName to "Other"
				end if
				try
					set TargetFolder to folder this_folderName of FilingCabinet
				on error
					set TargetFolder to (make new folder at FilingCabinet with properties {name:this_folderName})
				end try
				move ThisItem to TargetFolder
			on error msg
				-- Amongst the possible errors:
				-- "Finder got an error: Can't get name extension of ..."  (ThisFile might be a folder.)
				-- "An item of the same name already exists in the destination."
				display dialog msg buttons {"OK"} default button 1 with icon caution
			end try
		end repeat
	end tell
end adding folder items to

Edit: Just reading your original post again, if you wanted to move images to your Pictures folder instead of to “Images” in “Filing Cabinet”, you could rearrange the ‘ifs’ like this:

property imageList : {"gif", "jpg", "psd", "tif", "pic", "pict", "jpeg"}
property textList : {"txt", "doc", "rtf", "clpt", "tab", "sql", "xls"}
property pdfList : {"pdf"}
property soundsList : {"wav", "mp3", "m4a", "aif", "aiff"}
property macList : {"sit", "dmg", "zip", "bin", "hqx", "sea", "cpt", "arc", "z", "gz", "tar"}
property movieList : {"mov", "avi", "mpg", "MooV", "MPEG", "3gp", "wmv"}
property appList : {"appl"}
property scptList : {"scpt", "workflow"}
property internetList : {"webloc"}
property widgetList : {"wdgt"}

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		try
			set FilingCabinet to folder "Filing Cabinet" of home
		on error
			set FilingCabinet to (make new folder at home with properties {name:"Filing Cabinet"})
		end try
		repeat with ThisItem in added_items
			try
				set {TheExtension, TheFileType} to {name extension, file type} of item ThisItem
				if ((imageList contains TheExtension) or (imageList contains TheFileType)) then
					move ThisItem to folder "Pictures" of home
				else
					if ((textList contains TheExtension) or (textList contains TheFileType)) then
						set this_folderName to "Documents"
					else if ((pdfList contains TheExtension) or (pdfList contains TheFileType)) then
						set this_folderName to "PDF files"
					else if ((soundsList contains TheExtension) or (soundsList contains TheFileType)) then
						set this_folderName to "Music & Sounds"
					else if ((macList contains TheExtension) or (macList contains TheFileType)) then
						set this_folderName to "Disk Images & Archives"
					else if ((movieList contains TheExtension) or (movieList contains TheFileType)) then
						set this_folderName to "Movies"
					else if ((appList contains TheExtension) or (appList contains TheFileType)) then
						set this_folderName to "Application files"
					else if ((scptList contains TheExtension) or (scptList contains TheFileType)) then
						set this_folderName to "Scripts & Workflows"
					else if ((internetList contains TheExtension) or (internetList contains TheFileType)) then
						set this_folderName to "Internet"
					else if ((widgetList contains TheExtension) or (widgetList contains TheFileType)) then
						set this_folderName to "Widgets"
					else
						set this_folderName to "Other"
					end if
					try
						set TargetFolder to folder this_folderName of FilingCabinet
					on error
						set TargetFolder to (make new folder at FilingCabinet with properties {name:this_folderName})
					end try
					move ThisItem to TargetFolder
				end if
			on error msg
				-- Amongst the possible errors:
				-- "Finder got an error: Can't get name extension of ..."  (ThisFile might be a folder.)
				-- "An item of the same name already exists in the destination."
				display dialog msg buttons {"OK"} default button 1 with icon caution
			end try
		end repeat
	end tell
end adding folder items to

many thanks guys. i’ve actually learnt a lot about scripting from this project! i’m still experimenting with different solutions but you’ve been very helpful. i may get back to you with what i end up with! cheers :slight_smile:

Someone tell Sal/AppleScript Team to add in Switch/Select - Case statements. Looking at 4 or more nested Else Ifs drives me nuts.

Well, a repeat could be used instead. It would make maintaining and updating the script easier:

property imageList : {"gif", "jpg", "psd", "tif", "pic", "pict", "jpeg"}
property textRecord : {checkList:{"txt", "doc", "rtf", "clpt", "tab", "sql", "xls"}, folderName:"Documents"}
property pdfRecord : {checkList:{"pdf"}, folderName:"PDF files"}
property soundsRecord : {checkList:{"wav", "mp3", "m4a", "aif", "aiff"}, folderName:"Music & Sounds"}
property macRecord : {checkList:{"sit", "dmg", "zip", "bin", "hqx", "sea", "cpt", "arc", "z", "gz", "tar"}, folderName:"Disk Images & Archives"}
property movieRecord : {checkList:{"mov", "avi", "mpg", "MooV", "MPEG", "3gp", "wmv"}, folderName:"Movies"}
property appRecord : {checkList:{"appl"}, folderName:"application files"}
property scptRecord : {checkList:{"scpt", "workflow"}, folderName:"Scripts & Workflows"}
property internetRecord : {checkList:{"webloc"}, folderName:"Internet"}
property widgetRecord : {checkList:{"wdgt"}, folderName:"Widgets"}

property theRecords : {textRecord, pdfRecord, soundsRecord, macRecord, movieRecord, appRecord, scptRecord, internetRecord, widgetRecord}

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		try
			set FilingCabinet to folder "Filing Cabinet" of home
		on error
			set FilingCabinet to (make new folder at home with properties {name:"Filing Cabinet"})
		end try
		repeat with ThisItem in added_items
			try
				set {TheExtension, TheFileType} to {name extension, file type} of item ThisItem
				if ((imageList contains TheExtension) or (imageList contains TheFileType)) then
					move ThisItem to folder "Pictures" of home
				else
					set this_folderName to "Other"
					repeat with thisRecord in theRecords
						set thisList to thisRecord's checkList
						if ((thisList contains TheExtension) or (thisList contains TheFileType)) then
							set this_folderName to thisRecord's folderName
							exit repeat
						end if
					end repeat
					try
						set TargetFolder to folder this_folderName of FilingCabinet
					on error
						set TargetFolder to (make new folder at FilingCabinet with properties {name:this_folderName})
					end try
					move ThisItem to TargetFolder
				end if
			on error msg
				-- Amongst the possible errors:
				-- "Finder got an error: Can't get name extension of ..."  (ThisFile might be a folder.)
				-- "An item of the same name already exists in the destination."
				display dialog msg buttons {"OK"} default button 1 with icon caution
			end try
		end repeat
	end tell
end adding folder items to

Pure curiosity; academic interest:

In trying to “one-line” this:

set f to (choose file)
tell (info for f) to set {Nm, Ex, FT} to {name, name extension, file type}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
{BN, Ex, FT}

to this:

tell (info for (choose file)) to set BaseName to text 1 thru ((get offset of ("." & name extension) in name) - 1) of name
BaseName

it compiles, but fails with an error that the {full expansion of info for f} doesn’t understand the offset message. Is there a way around that?

tell (info for (choose file)) to set basename to name's text 1 thru ((my (offset of ("." & name extension) in name)) - 1)

:wink:

Bravo, and thanks. It was driving me nuts. “my” does it.:wink:

I admit I haven’t really looked at any of the scripts posted so far, but this script is what I use for my downloads. It tries to use the ‘type identifier’ of a file to, well, identify it, but can also rely on the kind or name extension.

--    Common Paths    --
property homePath : path to home folder as Unicode text
property libraryPath : path to library folder from user domain as Unicode text
property downloadsPath : (path to documents folder as Unicode text) & "Downloads:"

--    Ignored Kinds    --
property ignoredKinds : {"Safari download", "Widget"}

--    Identifier Properties    --
property appIdentifiers : {type identifier:{"com.apple.application-bundle"}, kind:{"Application"}, path:homePath & "Applications:Downloads:"}
property applescriptIdentifiers : {type identifier:{"com.apple.applescript.script"}, path:libraryPath & "Scripts:Downloads:"}
property movieIdentifiers : {type identifier:{"public.avi", "public.mpeg", "public.mpeg-4", "com.apple.quicktime-movie"}, kind:{"Shockwave Flash Movie"}, path:(path to movies folder as Unicode text) & "Downloads:"}
property imageIdentifiers : {type identifier:{"public.tiff", "public.jpeg"}, path:(path to pictures folder as Unicode text) & "Downloads:"}
property audioIdentifiers : {type identifier:{"public.mp3", "public.aifc-audio"}, path:(path to music folder as Unicode text) & "Downloads:"}
property pdfIdentifiers : {type identifier:{"com.adobe.pdf"}, path:downloadsPath & "PDFs:"}
property projectIdentifiers : {type identifier:{"com.apple.xcode.project"}, path:homePath & "Projects:Downloads:"}
property sourceIdentifiers : {type identifier:{"public.objective-c-source", "public.c-source", "public.c-header", "com.apple.applescript.text", "com.sun.java-source"}, path:homePath & "Projects:Downloads:"}
property archiveIdentifers : {type identifier:{"org.gnu.gnu-zip-archive", "com.pkware.zip-archive", "public.tar-archive", "com.apple.disk-image-udif", "com.allume.stuffit-archive"}, kind:{"bzip2 compressed archive", "disk image"}, name extension:{"bz2"}, path:downloadsPath & "Installers:"}
property webIdentifiers : {type identifier:{"public.html", "public.php-script"}, name extension:{"css"}, path:homePath & "Projects:Downloads:"}

--    Identifier Lists - Rank in order of preference    --
property folderIdentifiers : {projectIdentifiers, appIdentifiers, movieIdentifiers, imageIdentifiers, audioIdentifiers, sourceIdentifiers, webIdentifiers}
property fileIdentifiers : {appIdentifiers, archiveIdentifers, movieIdentifiers, imageIdentifiers, audioIdentifiers, pdfIdentifiers, applescriptIdentifiers, sourceIdentifiers, webIdentifiers}

--    On Adding Folder Items    --
on adding folder items to thisFolder after receiving theseItems
	matchAndMoveItems(theseItems)
end adding folder items to

--    On Open    --
on open theseItems
	matchAndMoveItems(theseItems)
end open

--    On Run    --
on run
	tell application "Finder" to set theseItems to selection
	matchAndMoveItems(theseItems)
end run


on matchAndMoveItems(theseItems)
	repeat with thisItem in theseItems
		set thisItem to thisItem as alias
		set itemInfo to info for thisItem without size
		set itemKind to kind of itemInfo
		
		if itemKind is in ignoredKinds then
			return
		else if itemKind is "Folder" then
			set moveFolder to findMovePathForFolder(thisItem)
		else
			set moveFolder to findMovePathForFileInfo(itemInfo)
		end if
		if moveFolder is false then return
		tell application "Finder"
			if not (moveFolder exists) then do shell script "mkdir " & quoted form of POSIX path of moveFolder
			move thisItem to folder moveFolder
		end tell
	end repeat
end matchAndMoveItems

on findMovePathForFolder(aFolder)
	tell application "System Events" to repeat with thisIdentifier in folderIdentifiers
		if my recordContainItem(thisIdentifier, {type identifier:missing value}) then
			repeat with thisType in type identifier of thisIdentifier
				if type identifier of every file of aFolder contains thisType then return contents of path of thisIdentifier
			end repeat
		else if my recordContainItem(thisIdentifier, {kind:missing value}) then
			repeat with thisType in kind of thisIdentifier
				if kind of every file of aFolder contains thisType then return contents of path of thisIdentifier
			end repeat
		else if my recordContainItem(thisIdentifier, {name extension:missing value}) then
			repeat with thisType in name extension of thisIdentifier
				if name extension of every file of aFolder contains thisType then return contents of path of thisIdentifier
			end repeat
		end if
	end repeat
	return false
end findMovePathForFolder

on findMovePathForFileInfo(fileInfo)
	repeat with thisIdentifier in fileIdentifiers
		if my recordContainItem(thisIdentifier, {type identifier:missing value}) then
			repeat with thisType in type identifier of thisIdentifier
				if type identifier of fileInfo is contents of thisType then return path of thisIdentifier
			end repeat
		else if my recordContainItem(thisIdentifier, {kind:missing value}) then
			repeat with thisType in kind of thisIdentifier
				if kind of fileInfo is contents of thisType then return path of thisIdentifier
			end repeat
		else if my recordContainItem(thisIdentifier, {name extension:missing value}) then
			repeat with thisType in name extension of thisIdentifier
				if name extension of fileInfo is contents of thisType then return path of thisIdentifier
			end repeat
		end if
	end repeat
	return false
end findMovePathForFileInfo

on recordContainItem(aRecord, recordItem)
	-- ignores if value is already 'missing value'
	return (aRecord & recordItem) does not contain recordItem
	-- considers if value is already 'missing value'
	---- return (aRecord & {||:null} & recordItem as list) does not end with recordItem
end recordContainItem

As for your question Adam, I was going to say ‘just add my’, but I see kai’s has taken care of it. :wink:

Personally, I would just use a simpler count of (or length of) to strip the extension off:

tell (info for (choose file) without size) to text 1 thru -((length of (get name extension)) + 2) of name

Then again, displayed name could be even easier, but depends of the Finder’s option for ‘Show all file extensions’ being off:

tell (info for (choose file) without size) to displayed name

Um… hope you’re not planning to use that on any files that don’t have an extension, Qwerty. :wink:

… which, with the possible exception of one’s own machine, really can’t be relied upon. :confused:

Hey, thanks kai :smiley:
Um… that was left as exercise for the reader! :wink:

Sigh I guess I’ll have to use an if statement then:

tell (info for (choose file) without size) to if name extension is missing value then
	name
else
	text 1 thru -((count of (get name extension)) + 2) of name
end if

You could also use a slightly modified one liner:

tell (info for (choose file) without size) to contents of item (((name ends with name extension) as integer) + 1) of {name, a reference to text 1 thru -((count of (get name extension)) + 2) of name}

Or maybe just a bit of dodgy one:

tell {first Unicode text, count of second Unicode text} of ({name, name extension} of (info for (choose file) without size) & {"" as Unicode text}) to text 1 thru (1 div (end + 1) - 2 - end) of beginning

BTW, can you see anything wrong with my script that I posted? Or any improvements?

Also, the number of ‘text items’ in the name depends on the the current value of AppleScript’s text item delimiters, as does the result of the coercion to string ” if the process gets that far. The “set BN .” line above only works when the delimiters have their default value of {“”} or “”.

Thank you for the additional ‘tweak’ about TIDs. Because I am very careful normally to grab TIDs, do stuff, then set them back, I always assume (optimistically) that they will be “” if they’re supposed to be.