help with populating a table

When I execute the code below. I get an error that reads “Finder got an error: Can’t continue FindType. (-1708)”
Any hints on what I can change?


tell application "Finder"
			set theFiles to every file of folder "FilesForApp" of desktop
			set theTableViewContents to {}
			repeat with a from 1 to length of theFiles
				set theCurrentFile to item a of theFiles
				set theCurrentFileName to name of theCurrentFile
				set testType to FindType()
				set end of theTableViewContents to {theCurrentFileName, testType, modification date of theCurrentFile}
			end repeat
		end tell

set content of table view 1 of scroll view 1 of drawer 0 of window "MainWindow" to theTableViewContents
		
on FindType()
	return {do shell script "grep blah blah blah"}
end FindType

Thanks in advance!

To refer to the script’s handler(s) while inside an application-targeted tell block, use my handlerName(.) or handlerName(.) of me. The error message is because the tell app “Finder” context of the call to FindType causes the script to try to tell Finder to run a command called “FindType”.

doh! Thanks.


tell application "Finder" to set theFiles to every file of folder "FilesForApp" of desktop
set theTableViewContents to {}
repeat with theCurrentFile in theFiles
	set testType to FindType()
	tell theCurrentFile to set end of theTableViewContents to {its name, testType, its modification date}
end repeat

set content of table view 1 of scroll view 1 of drawer 0 of window "MainWindow" to theTableViewContents

on FindType()
	return {do shell script "grep blah blah blah"}
end FindType

I finally got a chance to work this out some more and I’m having another issue :frowning:

Ok so here it is …
This part of the application will look at the contents of a folder and populate a table based on the files in that folder. The part I am having issues with is when I try to do a shell script … I get an error that reads something like:

Can’t make file («class docf» “FirstTest” of «class cfol» “appDB” of «class cfol» “Desktop” of «class cfol» “usejoe” of «class cfol» “Users” of «class sdsk» of application “Finder”) of «script» into type Unicode text. (-1700)


		tell application "Finder"
			set theFiles to every file of folder "appDB" of desktop
		end tell
		
		set theTableViewContents to {}
		repeat with a from 1 to length of theFiles
			set theCurrentFile to item a of theFiles
			set theCurrentFileName to name of theCurrentFile
			set foundTestType to my FindType(theCurrentFile)
			set testType to foundTestType
			set displayedDate to modification date of theCurrentFile
			set end of theTableViewContents to {theCurrentFileName, testType, displayedDate}
		end repeat
	
		set content of table view 1 of scroll view 1 of drawer 0 of window "mainWindow" to theTableViewContents

on FindType(theCurrentFile)
	return (do shell script "grep  -w Performed" & space & file theCurrentFile)
end FindType



This is pretty much the last thing I need to figure out before my app is done for now. Any suggestions? Thanks in advance.

Hi,

the variable theCurrentFile contains a Finder file specifier,
you should coerce it to a path string (or for a shell script the POSIX equivalent),
but I’ m not sure what you’re going to do with the grep line


tell application "Finder"
	set theFiles to every file of folder "appDB" of desktop
end tell

set theTableViewContents to {}
repeat with a from 1 to length of theFiles
	set theCurrentFile to item a of theFiles
	set theCurrentFileName to name of theCurrentFile
	set foundTestType to my FindType(POSIX path of (theCurrentFile as text))
	set testType to foundTestType
	set displayedDate to modification date of theCurrentFile
	set end of theTableViewContents to {theCurrentFileName, testType, displayedDate}
end repeat

-- set content of table view 1 of scroll view 1 of drawer 0 of window "mainWindow" to theTableViewContents

on FindType(theCurrentFile)
	return (do shell script "grep  -w Performed" & space & quoted form of theCurrentFile)
end FindType

I should’ve mention that in my first post. I want the grep line to search for the string “Performed” in theCurrentFile. So as the repeat statement is executed for the length of the files … a grep command is executed each time for each file. Then I want to display the resulting or matching string line inside of the table.

A shell script call in a repeat loop should be avoided if possible.
This script does almost the same, but much faster.
It reports the same result as the shell command


tell application "Finder"
	set theFiles to every file of folder "appDB" of desktop
end tell

set theTableViewContents to {}
repeat with a from 1 to length of theFiles
	set theCurrentFile to item a of theFiles
	set theCurrentFileName to name of theCurrentFile
	if (theCurrentFile as text) contains "Performed" then
		set displayedDate to modification date of theCurrentFile
		set end of theTableViewContents to {theCurrentFileName, "Binary file " & POSIX path of (theCurrentFile as text) & " matches", displayedDate}
	end if
end repeat

-- set content of table view 1 of scroll view 1 of drawer 0 of window "mainWindow" to theTableViewContents

Is there any way to get the entire line that is matched? Instead of just the matched word?

the result of both the grep and my version is the complete “line” which is actually the whole path of the file

hmm just tried compiling the script and the table doesn’t populate at all. I’m gonna dig around a bit and see what’s causing it to stop …
Thanks again StefanK

Maybe the braces are wrong, end of aList adds an item to the list


set end of theTableViewContents to (theCurrentFileName, "Binary file " & POSIX path of (theCurrentFile as text) & " matches", displayedDate)

hmm Xcode is automatically changing the braces to “{}”

Any ideas? This is the final piece of my project. Almost done!!

I think the problem lies in the “if” statement. If I remove the entire “if” statement and leave this line …

set end of theTableViewContents to {theCurrentFileName, "Binary file " & POSIX path of (theCurrentFile as text) & " matches", displayedDate}

the table is populated. Obviously , not populated correctly though. It prints out "Binary file “matches” in the table.

So something with this line may be wrong(?) …

if (theCurrentFile as text) contains "Performed" then

Hi,

I included the if - then lines, because I assumed you wanted to display only the files
with contain “performed” (grep filters only the matched items, if there is no match, you get an error message).

Perhaps you describe a bit more detailed, what you’re going to accomplish

I apologize. I probably was not too clear.

  1. There is a folder named MyAppDB on the desktop
  2. There are text files in this folder MyAppDB
  3. I want to search each text file for the word “Performed”
  4. The word performed will be on a line that reads something like “Performed: jumping test” or “Performed: sleep test”
  5. I want my script to search the contents of each text file in this folder and find “Performed:xxxxx”
  6. Then I want the exact text of “Performed: whatever test” to be put into a table.
  7. The table will have 3 columns { the file name, “performed:whatever test” matching string from the search, and the date modified of the file}

That’s all. Was that a slightly better description?

Aaaaaaaaahhhhh. Yes, indeed.
How should we know all these things???

Here a smart solution using Spotlight (assuming that the files are indexed)


set theFiles to paragraphs of (do shell script "mdfind -onlyin " & quoted form of ((POSIX path of (path to desktop) & "appDB")) & " kMDItemTextContent = \"Performed:\"")

set theTableViewContents to {}
repeat with theCurrentFile in theFiles
	set {name:theCurrentFileName, modification date:displayedDate} to info for (POSIX file theCurrentFile as alias)
	set testType to do shell script "cat " & quoted form of theCurrentFile & " | grep  -w Performed"
	set end of theTableViewContents to {theCurrentFileName, testType, displayedDate as text}
end repeat

set content of table view 1 of scroll view 1 of drawer 0 of window "mainWindow" to theTableViewContents

SOOO close! Your script returned all the content of the text file into the table cell. Just need to get the matching “Performed” line.

Hmm wait. I think there is something wrong with the text files I am creating in my app. Your script is correct if I try it with a different text file.
I’ll have to look into this portion. Thanks for the help!