Biggest, Weirdest, Loudest, Glitch -- And Really Painful!

I have a script. When I search for something (I searched for the word “pie”) my computer “freaks out” and the speakers go crazy with a noise that is very painful to my ears. When I close the script – everything goes back to normal. Here it is, search for something and prepare to mute your mac:

set thespotlightquery to text returned of (display dialog "What do you want to search?" default answer "" buttons {"Search!"} default button 1)
set spotlightquery to quoted form of thespotlightquery
set thefolders to {(path to applications folder as string), (path to home folder as string) & "Documents", (path to home folder as string) & "Downloads", (path to home folder as string) & "Desktop", (path to home folder as string) & "Movies", (path to home folder as string) & "Pictures", (path to home folder as string) & "Music"}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of (i as string)
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat
set numb to 1
set endfalse to false
if founditems is {} or founditems is {""} then
	display dialog "No search results!" buttons {"OK"} default button 1 with title "Your search for \"" & thespotlightquery & "\""
else
	repeat
		set i to item numb of founditems as string
		tell application "Finder" to set huh to exists (POSIX file i)
		if huh then
			set thesize to size of (get info for (POSIX file i))
			set thekind to kind of (get info for (POSIX file i))
			set thename to displayed name of (get info for (POSIX file i))
			set creation to creation date of (get info for (POSIX file i))
			set visibility to visible of (get info for (POSIX file i))
			set dialog to "Name: \"" & thename & "\"" & return & return & "Kind: " & thekind & return & return & "Size: " & thesize & " bytes" & return & return & "Creation Date: " & (creation as string) & return & return & "Visibility: " & (visibility as string) & return & return & "Path: " & i & return
			set button to (choose from list {"Open", "Show", "Peak at contents", "Back", "Next", "End"} with prompt dialog with title "Your search for \"" & spotlightquery & "\"") as string
		else
			set button to "Next"
		end if
		if button is "Open" then
			try
				set theapp to default application of (get info for (POSIX file i)) as string
				tell application theapp to open (POSIX file i as string)
				activate application theapp
			on error e
				display dialog "An error has occured trying to open your file:" & return & return & e buttons {"OK"} default button 1
			end try
			exit repeat
		else if button is "Back" then
			if numb is not 1 then
				set numb to numb - 1
			else
				beep
			end if
		else if button is "Next" then
			if numb is not (count founditems) then
				set numb to numb + 1
			else
				beep
			end if
		else if button is "End" then
			exit repeat
		else if button is "Show" then
			tell application "Finder"
				try
					select (POSIX file i as alias)
					activate
				on error e
					display dialog "An error has occured" & return & "trying to show you the file:" & return & return & return & e
				end try
			end tell
		else if button is "Peak at contents" then
			if name extension of (info for (POSIX file i as alias)) is in {"html", "strings", "txt", "m", "h", "applescript"} then
				set read_ to read (POSIX file i as alias) as string
				if (count read_) < 1300 then
					display dialog "Your file contents are as follows:" & return & return & read_ buttons {"OK"} default button 1
				else
					choose from list paragraphs of read_ OK button name "Finish" cancel button name "Finish" with prompt "Your file contents are as follows:"
				end if
			else
				display dialog "This file format is not currently supported with the read function of this application." buttons {"OK"} default button 1
			end if
		else if button is "false" then
			if endfalse is true then exit repeat
			set endfalse to true
			beep
		end if
	end repeat
end if

Wow, nice sound.

It just the beeps trying to beep a thousand times a second.

You need to re think how to incorporate them in your repeat loops.

And I think the problem is with one of your POSIX file coercion’s failing

**edit
The first one.

Also…

I think its the asking the finder to do the POSIX file coercion.

If I remember right, ‘POSIX file’ should not be asked for by finder as its a standard addition, and finder will throw a false even if it should be true.

Something like

set phuh to (POSIX file i)
tell application "Finder" to set huh to exists phuh

works

Hi,

Additionally to Mark’s explanation it’s not recommended to work with POSIX file class.
POSIX file is a file URL and most applications can’t handle this class.
For best compatibility coerce POSIX file always to alias or text.

You are the master of coercions :wink: Most of them are not needed at all.
For example:


set thefolders to {(path to applications folder as string), (path to home folder as string) & "Documents", (path to home folder as string) & "Downloads", (path to home folder as string) & "Desktop", (path to home folder as string) & "Movies", (path to home folder as string) & "Pictures", (path to home folder as string) & "Music"}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of (i as string)
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat

is the same as (there are direct relative paths for all folders in the list)


set thefolders to {path to applications folder, path to documents folder, path to downloads folder, path to desktop, path to movies folder, path to pictures folder, path to music folder}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of i
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat

an alias has a POSIX path property therefore coercing to text is not needed

Why do you check for existence of the file at all?? If Spotlight finds it, it obviously exists.
The coercion to POSIX file and get info for is called a lot of times,
this is quite expensive. The most effective way is to coerce the POSIX path to text and call info for once.

This is a version of your script “useless-coercion-free” :wink:


set thespotlightquery to text returned of (display dialog "What do you want to search?" default answer "" buttons {"Search!"} default button 1)
set spotlightquery to quoted form of thespotlightquery
set thefolders to {path to applications folder, path to documents folder, path to downloads folder, path to desktop, path to movies folder, path to pictures folder, path to music folder}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of i
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat
set numb to 1
set endfalse to false
if founditems is {} or founditems is {""} then
	display dialog "No search results!" buttons {"OK"} default button 1 with title "Your search for \"" & thespotlightquery & "\""
else
	repeat
		set theFile to POSIX file (item numb of founditems) as alias
		set {size:thesize, kind:thekind, name:thename, creation date:creation, visible:visibility, default application:theapp, name extension:theExtension} to info for theFile
		set dialog to "Name: \"" & thename & "\"" & return & return & "Kind: " & thekind & return & return & "Size: " & thesize & " bytes" & return & return & "Creation Date: " & (creation as text) & return & return & "Visibility: " & (visibility as text) & return & return & "Path: " & theFile & return
		set button to (choose from list {"Open", "Show", "Peak at contents", "Back", "Next", "End"} with prompt dialog with title "Your search for \"" & spotlightquery & "\"") as text
		if button is "Open" then
			try
				tell application theapp to open (theFile as text)
				activate application theapp
			on error e
				display dialog "An error has occured trying to open your file:" & return & return & e buttons {"OK"} default button 1
			end try
			exit repeat
		else if button is "Back" then
			if numb is not 1 then
				set numb to numb - 1
			else
				beep
			end if
		else if button is "Next" then
			if numb is not (count founditems) then
				set numb to numb + 1
			else
				beep
			end if
		else if button is "End" then
			exit repeat
		else if button is "Show" then
			tell application "Finder"
				try
					reveal theFile
					activate
				on error e
					display dialog "An error has occured" & return & "trying to show you the file:" & return & return & return & e
				end try
			end tell
		else if button is "Peak at contents" then
			if theExtension is in {"html", "strings", "txt", "m", "h", "applescript"} then
				set read_ to read theFile
				if (count read_) < 1300 then
					display dialog "Your file contents are as follows:" & return & return & read_ buttons {"OK"} default button 1
				else
					choose from list paragraphs of read_ OK button name "Finish" cancel button name "Finish" with prompt "Your file contents are as follows:"
				end if
			else
				display dialog "This file format is not currently supported with the read function of this application." buttons {"OK"} default button 1
			end if
		else if button is "false" then
			if endfalse is true then exit repeat
			set endfalse to true
			beep
		end if
	end repeat
end if

I wonder if you replace this:

else if button is "Peak at contents" then
           if theExtension is in {"html", "strings", "txt", "m", "h", "applescript"} then
               set read_ to read theFile
               if (count read_) < 1300 then
                   display dialog "Your file contents are as follows:" & return & return & read_ buttons {"OK"} default button 1
               else
                   choose from list paragraphs of read_ OK button name "Finish" cancel button name "Finish" with prompt "Your file contents are as follows:"
               end if
           else
               display dialog "This file format is not currently supported with the read function of this application." buttons {"OK"} default button 1
           end if
       else if button is "false" then

With this



else if button is "Peak at contents" then
			do shell script "/usr/bin/qlmanage -p " & quoted form of POSIX path of theFile
			
else if button is "false" then

would work better for your peak.

It will try and open the file in a quicklook like window using the quicklook generators you already have.
Which means more file types can be viewed.:slight_smile:

As I’m typing this with my iPhone, I can’t test these! I will later in the day.
I am happy that there is so many overnight posts! At least the sound will go away if these work!

Still – NOTHING! I’ve tried everything on this post! Maybe it is “mdfind?” Help! I just want to search in applescript FASHION!

I removed the “if huh then” thing. It stopped. Now I need to improve some more!

What do you mean ???

My problem is fixed. I removed the

set phuh to (POSIX file i)
tell application "Finder" to set huh to exists phuh
if huh then

and it now works.

Oh, that was just to explain, although it did work in your script when I tried it?

which problem?

I thought you would be using the StefanK version…

I am using that to play with your basic ideas.

One thing I noticed about StefanK version, was the Open with default application was broken.
A simple fix to the way he does it is just to throw in:

tell application "Finder" to set theapp to name of theapp

Oops, indeed, default application returns an alias, but a string path (or name) is required

Yeah, you have to have the name of an app or the path to it including the name as string. :smiley:

dylanweber,

I always find myself for one reason or other, tinkering with your scripts… :slight_smile:


set thespotlightquery to text returned of (display dialog "What do you want to search?" default answer "" buttons {"Search!"} default button 1)
set spotlightquery to quoted form of thespotlightquery
set thefolders to {path to applications folder, path to documents folder, path to downloads folder, path to desktop, path to movies folder, path to pictures folder, path to music folder}
set founditems to {}
repeat with i in thefolders
	set thepath to quoted form of POSIX path of i
	if exists thepath then
		set command to "mdfind -onlyin " & thepath & " " & spotlightquery
		set founditems to founditems & (paragraphs of (do shell script command))
	end if
end repeat
set numb to 1
set foundCount to 0
set dialog to ""
set button to ""
global numb, founditems, dialog, theFile, spotlightquery, button, theapp, foundCount

on getInfo()
	set theFile to POSIX file (item numb of founditems) as alias
	set {size:thesize, kind:thekind, name:thename, creation date:creation, visible:visibility, default application:theapp, name extension:theExtension} to info for theFile
	set dialog to "Name: \"" & thename & "\"" & return & return & "Kind: " & thekind & return & return & "Size: " & thesize & " bytes" & return & return & "Creation Date: " & (creation as text) & return & return & "Visibility: " & (visibility as text) & return & return & "Path: " & theFile & return
	
	set button to (choose from list {"Open", "Show", "Peak at contents", "Back", "Next", "Jump", "End"} with prompt ("Item [" & numb & "] of  [" & foundCount & "] found items") & return & return & dialog as text default items "Next" with title "Your search for \"" & spotlightquery & "\"") as text
	tell application "Finder" to set theapp to name of theapp
	my doSomthing(button)
end getInfo

if founditems is {} or founditems is {""} then
	display dialog "No search results!" buttons {"OK"} default button 1 with title "Your search for \"" & thespotlightquery & "\""
else
	set foundCount to (count of founditems)
	my getInfo()
	
end if

on doSomthing(button)
	if button is "Open" then
		try
			tell application theapp to open (theFile)
			activate application theapp
		on error e
			display dialog "An error has occured trying to open your file:" & return & return & e buttons {"OK"} default button 1
		end try
		
	else if button is "Back" then
		if numb is not 1 then
			set numb to numb - 1
			my getInfo()
		else
			beep
			my getInfo()
		end if
	else if button is "Jump" then
		my jump()
		my getInfo()
	else if button is "Next" then
		if numb is not foundCount then
			set numb to numb + 1
			my getInfo()
		else
			beep
			my getInfo()
		end if
	else if button is "End" then
		
	else if button is "Show" then
		tell application "Finder"
			try
				reveal theFile
				activate
			on error e
				display dialog "An error has occured" & return & "trying to show you the file:" & return & return & return & e
			end try
		end tell
	else if button is "Peak at contents" then
		do shell script "qlmanage -p " & quoted form of POSIX path of theFile
		my getInfo()
	else if button is "false" then
		
		beep
	end if
	
end doSomthing
on jump()
	display dialog "Enter item number to Jump to" & return & "[1 - " & foundCount & "]" default answer "1" buttons {"OK"} default button 1
	copy the result as list to {text_returned, button_pressed}
	try
		set numb to text_returned as number
		if numb > foundCount then
			my jump()
			beep
		else if numb is less than 1 then
			my jump()
			beep
		end if
	on error
		my jump()
		
		beep
		
	end try
end jump