Spotlight Buddies... bringing scripting languages into mainstream use

In a recent post, both Craig and Adam expressed some interest in possibly adding other scripting languages to the MacScripter discussions. Ruby, rb-appscript were the languages in particular. I think this forum is an excellent place to bring together these diverse scripting languages with the intent of actually “bringing them to the masses.” This may be a little geeky for some, but I think that there is a place in the Mac universe for short self contained applescript apps, ruby scripts, shell scripts or any combination thereof, which can be accessed like a macro using Spotlight. This would function much like Quicksilver, but without a separate 3rd party application. Lets face it, Spotlight in Leopard just rocks, and it is time to take advantage of this!

I have been informally testing this idea, and I have coined the phrase “Spotlight Buddies” for short scripts which perform useful day to day functions which are accessed via Spotlight. The beauty of this is that you can take advantage of several scripting languages to achieve the desired end. Then you just have to name your script/app with a unique name so that it is easily found with spotlight. So you’d name your “BurnCD” script “xxburncd”. Then you could access it relatively painlessly by typing cmd-spacebar, type the Spotlight buddy program’s name, wait a sec for the program to become the “top hit” and then hit return to run it. Simple. No extra application required.

There are a few small glitches which need to be addressed, but I really think that the MacScripter forum is the place to discuss and develop this idea. Shell scripting, ruby, applescript, automator workflows, rb-appscript could all play a role. There are a zillion little, short, easy to write scripts which could be easily accessed via spotlight – scale selected graphic files, rotate selected graphic files, select a file and have the script select the files which get closest to filling up your CD and automagically burn the disc (I’ve done this one already as a Ruby script)… the possible uses are limited only by your imagination.

The easy way to distribute these would be just to save the compiled apps or completed scripts here. There are other options which might be more interesting, but we can discuss that if the level of interest is high enough. There are some really talented people on this board, who I am sure could come up with some really useful scripts. Any takers?

vince

Hey vince I love the idea and would like to take part if we can get enough others on board!

Sounds interesting. I’ve long been interested in AS but only recently had occasion to make it do genuinely useful stuff. The ability to call shell scripts through AS is really cool - can other kinds of scripts (ie ruby) be called in a similar manner?

What are the strengths and weaknesses of the other scripting languages? And how about Python?

Um, we’ve been doing that here for years. Take a look around.

Everyone’s got their own way of getting at their favorite scripts. That being said, I also think that some ways are better than others. Personally, I think using spotlight as an application launcher is goofy, at best. There are so many other ways to launch scripts and apps that using such a clunky, roundabout method just to run a simple script seems like an incredible waste of time, energy, resources, etc… There are countless application launchers, status bar menus, stacks/dock menus, aliases, the script menu, and more tools that already exist to handle this task. I’m not sure how making a ‘spotlight buddy’ is any different than a plain script or application. All spotlight is going to do is hand off execution of the script to the finder/system anyways, so it’s less an issue of how the scripts are written/saved and really just an issue of how the item is named. I’m not sure how using an application to manage your scripts and apps is such a disadvantage. We use applications to do everything on a computer, so thinking you’re gaining something by using spotlight to organize your scripts is more limiting than simply having an app do it. I have hundreds of scripts on my computer. I use an application to manage the most frequently accessed ones, and keep a directory of scripts for the rest. I would spend more time trying to find a script with spotlight than if I just use the finder to navigate to my script directory.

I think the system here at macscripter, and at other forums, is already working pretty well. When possible, it’s best just to post code… whether whole scripts or just small examples… and let users write, organize, and name them in their own way. If you’re more protective of a script, distribute it as run-only in scriptbuilders or privately. Distributing a plain old script file and giving it a goofy name just because of an EXPECTATION that I’m going to search for it in spotlight is absurd. I do not use spotlight, other than via the finder’s find feature. I have completely disabled the spotlight icon and menu in the menu bar, and cmd-space does nothing for me. I consider using spotlight to launch apps as “fringe” behavior, and doubt it’s going to take off on a large scale. Why not distribute it with the expectation that I’m going to use it in one of the “normal” ways, and let me decide to rename it to ‘xyzDontGetGizmoWet’ if I want to hit cmd-space… wait… type that “quickly” into spotlight… wait… hope… select it… wait… and finally get to the script.

What I do agree with, is for macscripter to at least consider expanding it’s horizons. The idea of adding support for other languages has crossed my mind a number of times. While I know little about many of these other languages, it can’t hurt to have a place for people to expose them and learn about them. I would like to see some mechanism for contributing obj-c, primarily in a way that supports crossing over from AS to obj-c, but I realize it’s a bit out of place on an AS forum so I do what I can with what the site’s already got. Also, what you need are qualified people to contribute to and moderate those new areas, which may be difficult to find. I’m assuming that people using these other languages have found other communities in which to participate that are as specialized for each format as macscripter has been for AS. Sometimes less is more. A site like macscripter is always trying to find a healthy balance with content and focus, so expanding to be so inclusive may be a burden rather than a benefit.

I’m not trying to tell you how to manage your stuff, just don’t get all excited about the rest of us doing the same. Because you’re not actually doing anything different to your scripts to make them “spotlight buddies”, there’s no reason to even bother worrying about their name. Work harder to write good code and find a place to distribute it, and leave it to each individual how they want to use it.

Um, I’ve been on this board for a couple of years longer than you, and scripting since Applescript 1.0 and "The Tao of Applescript"days, and realize there are plenty of useful scripts. To be used with spotlight, for example, you’d need to recode it a little bit and add some error checking since the non-tech-saavy general users might want to try them. So, for a jpeg resize script, you’d configure it to work with the finder selection, and check the file type before running, etc. I’m suggesting that the scripts be configured so that the average Mac user can install them and use them…not just programmers.

Well, that’s your opinion. What’s so goofy about typing ‘cmd-spacebar, “Saf”, return’ to launch Safari? I don’t need an extra program, I don’t have to remember some obscure keystroke, I don’t have to worry about it breaking with the next system update… goofy? i think not… I have used Quickeys for years, and I can never remember the keystrokes to access all my apps. With Spotlight, I just have to remember the application’s name, type the first few letters and it is launched. If this is a ‘goofy fringe’ idea, well I guess I am in good company:
http://www.computerworld.com/action/article.do?command=printArticleBasic&articleId=9011005
http://iuseapple.com/blog/apple-how-to/beginner-os-x/2008/02/19/use-spotlight-as-an-application-launcher/
http://murphymac.com/spotlight-application-launcher/
http://lifehacker.com/392569/best-application-launchers
http://promacblog.com/posts/quick-leopard-report-spotlight-vs-quicksilver/
http://www.peachpit.com/guides/content.aspx?g=mac&seqNum=195
just a quick google search… I’m sure there are more…

Spoken like someone who hasn’t really tried my suggestion, or Spotlight very much. If you have Leopard, this works very efficiently. Try this: make a text file, name it xxTest, bury it in your scripts folder, and type xxTest in Spotlight. Is it really that much slower than your File manager or application Launcher or manual directory search?

This is a way to provide useful scripts to the general Mac user out there, to reach a wider base, and evangelize scripting languages as something which is another “Mac advantage”. I don’t expect everybody to use it (or even to “get it”, apparently), just as I don’t think that there will ever be a “programming lang ‘for the rest of us’”. I’d value your criticism a lot more if you weren’t so adversarial and prone to name calling. As it is, I think your unnecessarily hostile comments seem to suggest that perhaps you are on the fringe more than I. if you don’t want to participate, then don’t. If you have any constructive suggestions, and are willing to present constructive criticism without the name-calling, I’m more than willing to listen.

peace,
vince

You can do a lot with just applescript, don’t sell it short. Get proficient at Applescript, read about some unix commands and move on from there.

You can run ruby commands from the command line (and thus via ‘do shell’ in AS), but it gets a bit hairy if you have to escape a lot of quotes or special characters. You can also save ruby scripts as executable files and run them that way. It interjects a small delay, but saves you a lot of debugging time.

Ruby is pretty concise and is an object oriented language (everything is an object in ruby). Built in text routines, regular expressions, arrays and plenty of other fun stuff. There is lots of online info available – try Google.
I’m not familiar with Python.

vince

Name calling? When I call you a name, you’ll know it. Do you take all criticism this well, or only that which lends itself to the possibility that I might change my mind and someday tell you that your idea is actually fantastic and I was mistaken to question it at all? What has made things personal is that you should have avoided the impulse to throw how long you’ve been here and how long you’ve used a mac at me, and focused instead on what contributions you’ve made. I didn’t just start using a computer yesterday either, and I too have a bit of experience to back my criticism. I have written one of only two applescript mdimporters that exist (that I’m aware of), and have become fairly intimate with both spotlight and the spotlight cocoa api’s. Why you feel like you’re the only one who knows anything about spotlight is your business. Whether or not you like my demeanor, I feel my points are valid or I would not have taken the time to write them. I do tend to be fairly long-winded, and ultimately, I could have been more to the point and less judgmental… point taken… sorry to ruffle your feathers.

Don’t get me wrong, I think going through every script ever written, cleaning them up, and perfectly organizing them all in a nice, happy repository of scripts sounds… well, nice and happy. And I’m not being facetious. At some level we all want to do that with our own scripts, then we want to expand and organize other’s scripts, and then we want to change the world by giving the gift of scripts to everyone. It sounds good, but it’s a huge undertaking. If the current tool doesn’t work as you want it to, then it’s a natural progression to want to invent a better one. Simple. BUT… Why does it have to be done in the context of spotlight? That is really my only question. The whole point of my point was that there is no point to limiting yourself to ‘spotlight buddies’. Why not just clean up the scripts because they are scripts and market them as what they are… scripts? Sure, go ahead on your download page, in a forum thread, in your blog, whatever, and tell the world that one good way to launch any given script would be to use spotlight. Limiting your presentation to just use via spotlight will kill your intentions before you even get rolling. The title and entire focus of your post clearly illustrates that YOUR personal goal is to show how valuable the use of spotlight is to launch these scripts. Instead, your focus should be how valuable the scripts themselves are, and then let me, the user (however fringe or savvy I may be) decide how to use them. If your post’s content was, “Hey, let’s clean up all the scripts and make them available to general users as easily identifiable, categorized, well-implemented units that can be used in any number of ways!”, my first response would have been very different. Don’t expect everyone (or anyone) to use them in any given way. Make them portable, customizable (when possible), dynamic, and as powerful as possible without sacrificing clarity. If you’re going to bother to make something, why not make it something that anyone can use in any way, rather than something that some people can use in one way? By suggesting that it’s best used via spotlight, you’re likely to exclude others whether or not that is your intention. Just peddle them as scripts, and if you’re that bent on getting everyone to try spotlight then find another, more appropriate way of telling everyone.

Before you cast off and take everything I’ve said the wrong way, just take the time to consider the general messages I’ve buried beneath all the fluff. I am just trying to help you to clarify upfront exactly what the purpose is, so you don’t spend a lot of time going about things the wrong way. If anything, realize that you’ll touch a lot more people by offering something that at first glance is available and useful to EVERYONE… not just those who use spotlight.

Jed,

Clearly if you don’t like Spotlight, that’s fine with me. I can see then, that there really isn’t any point to wasting my time trying to convince you of its usefulness. I would submit to you that I’m not trying to undermine the usefulness of utility applescripts for you or anyone else on this board. The scripts can be freely modified to suit your purposes as well as mine. How you choose to use them is up to you. I’m just suggesting a way to perhaps attract new users to scripting applications in general, and to give them a relatively simple way to use them on a daily basis.

Now you might argue that Automator is already there for this purpose, but I’ll bet there are a ton more people who use and are comfortable with Spotlight than Automator workflows and plug-ins. I’m sure your opinion differs, so lets just agree to disagree and move on.

vince

James,

This is really an idea/concept which needs to be explored further. I’m open to suggestions. I noticed your posts on the Applescript mailing list re:ruby yesterday, and there are some issues with regard to running or distributing Ruby scripts. You could try running them via applescript, but the other options are:

  • distribute them as executable scripts, set the default application to “terminal” which will then run the script in Terminal. The advantage of this is that you can use Ruby “puts” statements to provide feedback to the user in the terminal window. Downside is that you open, and have to quit another app. I have a ruby script which I have tested in this fashion and would be glad to share it with you for further testing/development (it is still “beta”).
  • use Platypus to create a self running application (can use this for Ruby, shell, python scripts). I have downloaded Platypus but haven’t played with it yet.
  • other options (readers: provide input here…)

I think for the early testing of this concept, we might want to try a few small scripts for using Image Events to do something with Finder selected graphics files. Another potentially interesting application would be something that pastes text into whatever application is “frontmost”, so you could create a handful of scripts which could be used to insert your address or sig in any application.

I might have a few little test scripts cobbled together this weekend if I can find some spare time.

vince

Try this simple script. Selected graphics files in the Finder can be resized using the Spotlight Buddy script which is attached.

  1. compile the script as an application, give it a unique name, such as “xxscale50”
  2. save it somewhere
  3. select some COPIES of jpeg files in the Finder to try it on (N.B.: script will overwrite originals…use only on copies) Or you can run it on a folder…or a combo of folders and files.
  4. If using OS 10.5 (Leopard) run it with Spotlight by typing the following:

command-spacebar (brings up spotlight search field)
xxsca (enter your Spotlight buddy name in the Spotlight search field. If this is your only file with the ‘xx’ prefix,
you might be able to just type ‘xxs’ and it will be the top hit in the spotlight list.)
return (this will run the script – hitting return selects/opens the “Top Hit” in Spotlight, and your uniquely named application should be the top hit)


tell application "Finder"
	set finder_sel to (get selection)
	set file_list to {}
end tell

repeat with x in finder_sel
	set file_list to file_list & ((x) as alias)
end repeat

-- ? warning dialog, offer to copy files?

repeat with y in file_list
	if folder of (info for y) then
		set full_path_list to {}
		set file_name_list to list folder y without invisibles
		repeat with i in file_name_list
			set full_path_list to full_path_list & ((y as string) & i)
		end repeat
		tell application "Image Events"
			-- start the Image Events application
			launch
			repeat with p in full_path_list
				if (my check_file_type(file p)) then -- there was a problem here
					set this_image to open p
					-- perform action
					scale this_image by factor 0.5
					-- save the changes
					save this_image with icon
					-- purge the open image data
					close this_image
				else
					display dialog "File type detected which cannot be processed!" & return & "It will be ignored."
				end if
			end repeat
			
		end tell
		-- process folder
	else -- it is file?
		if (my check_file_type(y)) then
			try
				tell application "Image Events"
					-- start the Image Events application
					launch
					-- open the image file
					set this_image to open y
					-- perform action
					scale this_image by factor 0.5
					-- save the changes
					save this_image with icon
					-- purge the open image data
					close this_image
				end tell
			on error error_message
				display dialog error_message
			end try
		else
			display dialog "File type detected which cannot be processed!" & return & "It will be ignored."
		end if
	end if
end repeat

on check_file_type(tpath)
	if name extension of (info for tpath) is in {"jpg", "tiff", "png", "pict", "bmp", "gif", "psd"} then
		return true
	else
		return false
	end if
end check_file_type

v

Takers on what? Writing scripts?

Yes. Specifically, utility scripts which can be run via Spotlight. Applescript, shell scripts, ruby scripts, Automator workflow apps, etc which the user can access by a few quick keystrokes, similar to Quicksilver. This might only require some minimal reconfiguring of already available scripts so that they work with Finder selections or whatever.

  • graphic manipulation (scale, crop, rotate, etc)
  • text macros
  • file organization
  • access info from the web in the background and paste into a document
  • send a quick email without opening mail
  • post selected pictures to your idisk
  • whatever repetitive little annoying task you’d like a script to do for you while you do more important stuff

As jobu correctly points out, there are a lot of scripts here already. They might need to be modified so they can run from Spotlight mimicking Quicksilver. So they need to get their input in a manner different from drag-n-drop or selecting from a dialog (although dialog selection might be what you want in some instances). To make them more accessible to non-scripters, additional error checking would also be good.

vince

Here’s a rudimentary way to insert text into any document, which can be used for addresses, sigs, or longer repetitive text which you don’t want to retype.
Take the following script, compile it as an applescript application, save it with a unique name like “xxhomeadd” if this is your home address text macro. Then to run it, you will use Spotlight by typing cmd-spacebar, enter the first several letters of the filename, and return when it is the “top hit” in the Spotlight results. This will insert your home address in the current document where the cursor was placed when the script was run.

Note: running this file from the Finder by double clicking will give “Finder” as the frontmost application and the script won’t work. This must be run from spotlight.

Room for improvement: the method used to get the frontmost application causes the frontmost app to deactivate and activate briefly. But the script works.

vince

Here’s the script:


set myName to "John Q. Public"
set myStreet to "123 Main St."
set myCity to "Anywhere"
set myState to "PA"
set myZip to "18452"
set mydivider to "=============="
set myURL to "http://www.myurl.com"
set myemail to "jqp@mac.com"
set Paste_content to myName & return & myStreet & return & myCity & ", " & myState & " " & myZip & return & mydivider & return & myURL & return & myemail & return


tell application "System Events"
	set visible of (first application process whose frontmost is true) to false
	set frontApp to item 1 of (get name of processes whose frontmost is true)
	activate
end tell

tell application frontApp
	activate
	set the clipboard to Paste_content
	
end tell


tell application "System Events"
	tell application process frontApp
		keystroke "v" using {command down}
	end tell
end tell

Just sayin…

http://9to5mac.com/2015/04/14/flashlight-spotlight-plugins/