File Type problems (POSIX path/file/etc)

I’m working on a program where I am collecting folders to process by drag and drop to the application and a table in the application window. I seem to have this working fine. There is a routine I am using that chrys put together to get a file list using shell scripting which works great. I am trying to call the same routine in another part of the program and it does not seem to work and I think that it might be tied to the stored value for the folder. I have this routine for files dropped on the application:

on open droppedFiles
	log "On Open"
	set theFiles to {}
	if class of droppedFiles is not list then set droppedFiles to {droppedFiles}
	repeat with i from 1 to count of droppedFiles
		copy (POSIX path of (item i of droppedFiles)) to end of theFiles
	end repeat
end open

It calls this routine which adds the paths to a table and a property if the dropped item is a folder. The find file command in the loaded script library works great here.

to SetTableData(FolderList)
	set update views of FolderData to false -- Turn off the updating of the views
	-- For every item in the list, make a new data row and set it's contents
	set foundFiles to {}
	repeat with theItem in FolderList
		set theItem to theItem as string
		set PathList to contents of data cell "Path" of every data row of FolderData
		if (theItem as string) is not in PathList then --
			set ItemKind to kind of (info for (POSIX file theItem) without size)
			if ItemKind is "Folder" then
				copy theItem to end of FoldersToProcess
				set theDataRow to make new data row at end of data rows of FolderData
				set contents of data cell "Path" of theDataRow to (theItem)
				set MyFileTypes to getFileTypes()
				tell ScriptLib to copy FindFiles(theItem, MyFileTypes) to end of foundFiles
				set ImageCount to length of last item of foundFiles
				set contents of data cell "Images" of theDataRow to ImageCount
			end if
		end if
	end repeat
	set update views of FolderData to true -- Turn back on the updating of the views
end SetTableData

I’m calling it again here:

set RunStatus to "Running"
set FolderCount to count of FoldersToProcess
log {FolderCount, FoldersToProcess}
set label of theObject to "Pause"
set image name of theObject to "pauseICN"
repeat with i from 1 to FolderCount
	set currentFolder to item i of FoldersToProcess
	set MyFileTypes to getFileTypes()
	tell ScriptLib to set FilesToProcess to FindFiles(currentFolder, MyFileTypes)
	log FilesToProcess
end repeat

The list FoldersToProcess is built in the previous routine from theItem. This is used sucessfully in using the find file routine (requiring a POSIX path). However, when I call it again nothing is returned. When logging the list and the individual items everything looks the same. Classes seem to be messed up in Xcode but when getting the class I get the same string of scientific notation in both places.

Am I missing something here? Is there some potential for a forced coercion that I am not seeing? I’m don’t usually use POSIX file/paths and am cursing their addition right now to the language for the added complexity.

Just for reference, is your FindFiles handler the one from this thread?

I think the problem is a in some unexpected behavior introduced by the way I originally wrote findFiles. If your current version still has a line like

if class of extensionList is not list then set extensionList to {extensionList}

at the start of FindFiles, try changing it to this:

if class of extensionList is not list then
	set extensionList to {extensionList}
	(* Duplicate the list, because it will be destructively modified by buildFind0Command.
	* We should not stomp on our caller's parameters unless it is clearly documented (like convertPOSIXPathsToFiles and convertItemsToAliases do).
	copy extensionList to extensionList
end if

BTW, this was the key information that let me find the bug. There are two parameters to findFiles. Since the handler worked once, and you verified the first parameter of the second call, the problem must be in the second parameter of the second call.

I apologize for this stupid bug and any frustration it has caused you. I will update the original post to fix this problem for future users.

Here is an example that demonstrates the issue you uncovered. My original code was like b (changes appear in the source list), the new code will be like c (changes do not appear in the source list).

set a to {1, 2, 3}
set b to a
copy a to c
set first item of a to "a"
set first item of b to "b"
set first item of c to "c"
{a, b, c} --> {{"b", 2, 3}, {"b", 2, 3}, {"c", 2, 3}} -- "b" is put into the a list!


That is the script, and thank you for updating it. It is a great solution for finding files.

Unfortunately I don’t think that was the problem. Fortunately I think that the problem is not a problem at all with the script.

In both handlers if I log the result of the handler FindFiles(startDirectory, extensionList) in the console it shows an empty list and this has caused me to spin my wheels for quite a while trying to figure out if it was a problem with the extension list or the path being passed to the script.

Playing around with it some more this morning after applying your updates I find that there is a list there to work with. I can get a length of it as well as the items out of it. But it logs as {} in the console. It looks like this is happing because I took off the convert to aliases and the list is not logging because the items of the list are POSIX files, which seems odd to me for the console not being able to display in the log.


So everything works, but the log output was misleading you into thinking it was not working? Sigh.

I learned to avoid log since (in my experience) it always seems to flatten lists and records, dereference references and generally misrepresent the data passed to it. I do still use it for log “got to point A” debugging though. In Script Editor I like to arrange for the values of interest to be returned since the Result display does (more) accurately represent values of most all variations.

But I do not think something like that would work for AppleScript Studio. You could do something using load script and store script to stash away actual AppleScript values in the loaded/stored script’s properties. But it would be a bit cumbersome in operation (both logging and log viewing).

Yes, everything is there and if I turn on the convert to alias function it logs fine, it just logs an empty list if I leave that step out and return the list of POSIX files. I was trying to simplify things and eliminate the added coercion of the list. I’m not really used to working with POSIX files but thought it might be best to stay with one type of value unless the other was needed.

I have used the return method a lot in working out handlers which works good for that. The load/store script seems a bit much. I did contemplate using something like this as a method of storing persistent values in records but since I reworked my UI to bind to user defaults this was not necessary, and the binding simplified the script as well as eliminated a good deal of debugging I was going through with the previous version of the application. Instead I have a data file that I made to store the values which gives me the added benefit of having a ready method for exporting/importing the stored data to a file that can be easily edited in a text editor.