Miscellanous scripts

Hello.

I saved it as an applet, with the name ghead, and set LSUIElements to 1 in its plist file to make it work from spotlight, and quicksilver flawlessly as well.

In the script menu the “script” looks like this :slight_smile:

tell application "ghead" to run

Hello.

I have packed the shell script into the applet, that can be downloaded from here.

Enjoy!

Hello.

I finally figured out how to use the app from an Automator Service as painlessly as possible.

First I open Automater, and makes a new service, that shall process marked text.

Secondly I add an exececute shell script action which should get the content below.

 open -a "ghead"

Save it, as GetHeader for instance, and you are good to go open header files from the services menuitem of the contextual menu. :slight_smile:

Hello.

I am very picky about windows retaining their focus, after an app, or a script has been run.

Well, now this is script should be fixed, as far as I know, I use UI scripting to make it happen like this:


tell application thNm to activate
	tell application id "sevs"
		click window 1 of application process thNm
	end tell
	error number -128
end tell

The app in the link. is updated.

Hello.

I made a little change while I were at it:

Now it will ask you, if it couldn’t find any header file the first time around (misspelling).

It can still be found here.

And as far as I know, I am done with it. I won’t write help file for this, so the basic idea is to mark or input the name of a header file, say stdio.h, or CFSet.h or something, and it will pop up in quicklook right in front of you. If XCode is still the default editor for c and h files, then the header file will pop up into XCode if you double click on it. :slight_smile:

Hello.

I must say I am fairly happy with the applet for displaying header files, so I thought I’d wrap the gman script: A shell script for using quicklook to display manual pages into the same kind of applet. Here it is

You make a service for it to execute the applet from the contextual menu the same way as described in post #8.

If the manual page looks like it is html formatted, then chances are big that the manual page will pop up in Safari or your default web-browser, should you double click on it in quicklook.

Enjoy

Hello.

I fixed a bug in ghead.app, concering having marked something that doesn’t go for a header file.

The new version can be found It can still be found here.

I’ll most probably have to do the same fix in gman.app as well.

Hello.

I have done something more to the logic of ghead, so that it won’t be baffled by a selection in your active window, that doesn’t return a result.

I have also tried to speed it a little bit up, but I realize I have to write the history to disk, to get it to be more responsive, and it isn’t that bad really. But who knows, those computer seconds do take a long long time. :slight_smile:

PS. People who use this are also supposed to have activity monitor open. :slight_smile: But now it dies after 2 minutes of inactivity, should the dialog of the faceless app disappear, without you being inclined to hide the frontmost apps to find it.

Due to the changes I have made, I don’t think that to be an issue anymore anyway.

Hello.

A little script that shows the folders of a directory tree that contains scpt, applescript or scpt files, in a TextEdit window, then it asks you, and lets you open one of the folders if you want to.

It doesn’t play well with filenames that contains “.” (ellipsis) and such.

If you have your script folders in just one place, it would be practical, to subsitute a hard path, for the choose folder dialog it starts with.

-- Copyright 2013 © McUsr and put into public domain

set toolbarUtilitiesIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ToolbarUtilitiesFolderIcon.icns") as alias
set pxpath to POSIX path of (choose folder)
set theDirs to do shell script "rootFol=" & quoted form of pxpath & " ;cd $rootFol; ( echo \"ScriptFolders in \"$rootFol ; echo  \"=============================================================
\" ;mdfind -onlyin . \"kMDItemFSName == '*.scpt' || kMDItemFSName ='*.scptd'|| kMDItemFSName ='*.applescript' \" | /usr/bin/sed -n 's_'\"$rootFol\"'\\(..*[/]\\)\\([^/][^/]*\\)$_./\\1_p' | awk '{ dir[$0]=$0 } END { for (a in dir ) print a  }' |sort )"

tell application "TextEdit"
	make new document
	set text of document 1 to theDirs
end tell
tell application "TextEdit" to activate

try
	tell application "SystemUIServer"
		activate
		display dialog "Would you like to open a folder?" with title "Script Folder Lister" buttons {"Cancel", "Yes"} default button 2 with icon toolbarUtilitiesIcon
	end tell
on error
	tell application "TextEdit" to activate
	error number -128
end try

set theDirList to paragraphs of (do shell script "rootFol=" & quoted form of pxpath & " ; tr -s '
' '
' <<<" & quoted form of theDirs & " | /usr/bin/sed -n '1,3 ! s_\\./\\(.*\\)_'\"$rootFol\"'\\1_p'")

tell application "SystemUIServer"
	activate
	set toOpen to (choose from list theDirList default items item 1 of theDirList with prompt "Choose a ScriptFolder to Open." with title "Script Folder Lister")
end tell
if toOpen is not false then
	do shell script "open " & quoted form of (toOpen as text)
	tell application "Finder" to activate
else
	tell application "TextEdit" to activate
end if

Hello.

This is the version for c and m files, do you need M then add : " || kMDItem == ‘*.H’ " to the mditem clause, and similarily for cpp.

-- Copyright 2013 © McUsr and put into public domain
property scriptTitle : "C-Source files"
set toolbarUtilitiesIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ToolbarUtilitiesFolderIcon.icns") as alias
set pxPath to POSIX path of (choose folder)
set theDirs to do shell script "rootFol=" & quoted form of pxPath & " ;cd $rootFol; ( echo \"" & scriptTitle & " in \"$rootFol ; echo  \"=============================================================
\" ;mdfind -onlyin . \"kMDItemFSName == '*.c' || kMDItemFSName ='*.m' \" | /usr/bin/sed -n 's_'\"$rootFol\"'\\(..*[/]\\)\\([^/][^/]*\\)$_./\\1_p' | awk '{ dir[$0]=$0 } END { for (a in dir ) print a  }' |sort )"

tell application "TextEdit"
	make new document
	set text of document 1 to theDirs
end tell
tell application "TextEdit" to activate

try
	tell application "SystemUIServer"
		activate
		display dialog "Would you like to open a folder?" with title scriptTitle & " from " & pxPath buttons {"Cancel", "Yes"} default button 2 with icon toolbarUtilitiesIcon
	end tell
on error
	tell application "TextEdit" to activate
	error number -128
end try

set theDirList to paragraphs of (do shell script "rootFol=" & quoted form of pxPath & " ; tr -s '
' '
' <<<" & quoted form of theDirs & " | /usr/bin/sed -n '1,3 ! s_\\./\\(.*\\)_'\"$rootFol\"'\\1_p'")

tell application "SystemUIServer"
	activate
	set toOpen to (choose from list theDirList default items item 1 of theDirList with prompt "Choose a ScriptFolder to Open." with title scriptTitle & " from " & pxPath)
end tell
if toOpen is not false then
	do shell script "open " & quoted form of (toOpen as text)
	tell application "Finder" to activate
else
	tell application "TextEdit" to activate
end if

While I am at it:

Here is one, you’ll like if you need to look at your own header files from within XCode, you just change the property revalInTextEdit to false, if you think an open dialog will suffice. You just change the script to use a posix path, if you have your stuff in one place, and customize it further, if you want to get at your locall library source files.

-- Copyright 2013 © McUsr and put into public domain
property revalInTextEdit : false
property scriptTitle : "Private Header Files"
set pxpath to POSIX path of (choose folder)
set toolbarUtilitiesIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ToolbarUtilitiesFolderIcon.icns") as alias
set pxPath to "~/include"
set theDirs to do shell script "rootFol=" & pxPath & " ; cd $rootFol; ( echo \"" & scriptTitle & "s in: \"$rootFol ; echo  \"=============================================================
\" ;mdfind -onlyin . \"kMDItemFSName == '*.h	'\" |sort )"

if revalInTextEdit then
	tell application "TextEdit"
		make new document
		set text of document 1 to theDirs
	end tell
	tell application "TextEdit" to activate
	
	try
		tell application "SystemUIServer"
			activate
			display dialog "Would you like to open a " & scriptTitle & " from " & pxPath & "?" with title my scriptTitle & " from " & pxPath buttons {"Cancel", "Yes"} default button 2 with icon toolbarUtilitiesIcon
		end tell
	on error
		tell application "TextEdit" to activate
		error number -128
	end try
end if
set theDirList to paragraphs of (do shell script "tr -s '
' '
' <<<" & quoted form of theDirs)

tell application "SystemUIServer"
	activate
	set toOpen to (choose from list theDirList default items item 1 of theDirList with prompt "Choose the " & scriptTitle & "  from " & pxPath & " to open." with title my scriptTitle & " from " & pxPath)
end tell
if toOpen is not false then
	do shell script "open " & quoted form of (toOpen as text)
	tell application "Xcode" to activate
else
	if revalInTextEdit then
		tell application "TextEdit" to activate
	else
		local nm
		tell application "System Events" to set nm to (get name of first application process whose visible is true and frontmost is true)
		tell application nm to activate
	end if
end if

I am still at it, here is one that lets you search for headerfiles, containing an identifer by a regex.

In /System/Library/Frameworks

-- Copyright 2013 © McUsr and put into public domain
property revalInTextEdit : false
property searchWord : ""
property scriptTitle : "/System/Library/Frameworks"
set toolbarUtilitiesIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ToolbarUtilitiesFolderIcon.icns") as alias
local nm
tell application (path to frontmost application as text)
	set searchWord to text returned of (display dialog "Enter a search term, Miniminum one letter. Wildcards where you like them!" with title my scriptTitle default answer searchWord with icon toolbarUtilitiesIcon)
end tell
if searchWord = "" then
	tell application "System Events" to set nm to (get name of first application process whose visible is true and frontmost is true)
	tell application nm to activate
	error number -128
	
end if


set theFiles to do shell script "( echo \"" & scriptTitle & " Header files containing: \"\"" & searchWord & "\" ; echo  \"=============================================================
 \" ; mdfind -0 -onlyin " & scriptTitle & " \"kMDItemFSName == '*.h'\" |xargs -0 -I {} grep -l " & "\"" & searchWord & "\"" & " {} ); true "

if revalInTextEdit then
	tell application "TextEdit"
		make new document
		set text of document 1 to theFiles
	end tell
	tell application "TextEdit" to activate
	
	try
		tell application "SystemUIServer"
			activate
			display dialog "Would you like to open an include file?" with title "Include files in " & my scriptTitle buttons {"Cancel", "Yes"} default button 2 with icon toolbarUtilitiesIcon
		end tell
	on error
		tell application "TextEdit" to activate
		error number -128
	end try
end if
set theFilesList to paragraphs of (do shell script "tr -s '
' '
' <<<" & quoted form of theFiles)

tell application "SystemUIServer"
	activate
	set toOpen to (choose from list theFilesList default items item 1 of theFilesList with prompt "Choose the Include file to Open." with title "Include files in " & scriptTitle)
end tell
if toOpen is not false then
	do shell script "open " & quoted form of (toOpen as text)
	tell application "Xcode" to activate
else
	if revalInTextEdit then
		tell application "TextEdit" to activate
	else
		tell application "System Events" to set nm to (get name of first application process whose visible is true and frontmost is true)
		tell application nm to activate
	end if
end if

You can just change the property scriptTitle to point to a different include path, and save the script under another name.

Hello.

I have improved the three scripts in post #17 slightly with regards to search criteria, so that no double ticks, ment to escape the regexp are showing up in the dialog again.

Hello.

I have updated the scripts in post #14,#15#16 and #17 Mostly with regard to layout, and made it easy to duplicate the script in post #17 to suit your own needs for other dedicated paths, as you usually know which framework, your are looking for an identifier in. (That is why I haven’t made it into one slough with a choose from list for picking framework.

Hello.

I grabbed DJ Bazzie Wazzies script for using StefanK’s SKProgressbar in post #6 and modified it slightly, as I had to wait for getting the results, when searching for indentifiers.

StefanK’s SKProgress bar can be found here

I used this to modify the script in post #17 this thread, and the modded version is below.

I am pleased with the result! Enjoy! :wink:

-- Copyright 2013 © McUsr and put into public domain
-- StefanK's SKProgressBar: © StefanK and not in public domain for commercial purposes! http://macscripter.net/viewtopic.php?pid=160302#p160302
-- DJ Bazzie Wazzie © DJ Bazzie Wazzie SKProgressBar code http://macscripter.net/viewtopic.php?id=40763
property revalInTextEdit : false
property searchWord : ""
property scriptTitle : "/usr/include"
set toolbarUtilitiesIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ToolbarUtilitiesFolderIcon.icns") as alias
local nm
tell application (path to frontmost application as text)
	set searchWord to text returned of (display dialog "Enter a search term, Miniminum one letter. Wildcards where you like them!" with title my scriptTitle default answer searchWord with icon toolbarUtilitiesIcon)
end tell
if searchWord = "" then
	tell application "System Events" to set nm to (get name of first application process whose visible is true and frontmost is true)
	tell application nm to activate
	error number -128
	
end if
tell application "SKProgressBar"
	set floating to true
	set position to {600, 550}
	set width to 500.0
	set title to "Include files in " & scriptTitle
	set header to "Searching.."
	set header alignment to left
	set footer to "For identifers"
	set footer alignment to right
	set show window to true
	tell progress bar
		activate
		set indeterminate to true
		start animation
	end tell
end tell

set theFiles to do shell script "( echo \"" & scriptTitle & " Header files containing: \"\"" & searchWord & "\" ; echo  \"=============================================================
 \" ; mdfind -0 -onlyin " & scriptTitle & " \"kMDItemFSName == '*.h'\" |xargs -0 -I {} grep -l " & "\"" & searchWord & "\"" & " {} ); true "
tell application "SKProgressBar"
	stop animation
	set show window to false
	quit
end tell

if revalInTextEdit then
	tell application "TextEdit"
		make new document
		set text of document 1 to theFiles
	end tell
	tell application "TextEdit" to activate
	
	try
		tell application "SystemUIServer"
			activate
			display dialog "Would you like to open an include file?" with title "Include files in " & scriptTitle buttons {"Cancel", "Yes"} default button 2 with icon toolbarUtilitiesIcon
		end tell
	on error
		tell application "TextEdit" to activate
		error number -128
	end try
end if
set theFilesList to paragraphs of (do shell script "tr -s '
' '
' <<<" & quoted form of theFiles)

tell application "SystemUIServer"
	activate
	set toOpen to (choose from list theFilesList default items item 1 of theFilesList with prompt "Choose the Include file to Open." with title "Include files in " & scriptTitle)
end tell
if toOpen is not false then
	do shell script "open " & quoted form of (toOpen as text)
	tell application "Xcode" to activate
else
	if revalInTextEdit then
		tell application "TextEdit" to activate
	else
		tell application "System Events" to set nm to (get name of first application process whose visible is true and frontmost is true)
		tell application nm to activate
	end if
end if



Hello

For those of you fortunate enough to have such a new version of OS X that your Library aren’t indexed by spotlight anymore, please do try this:

do shell script " mdimport ~/Library"

I have gotten that line from Technical Q&A QA1475: Tips for searching Headers, APIs and ADC reference material.

But it should work equally well for stuff stored in the Library folder. :slight_smile:

The Document just mentioned, puts the scripts in the posts above in a whole new light, making them less necessary, once you have read and adapted to the Q&A, but there should still be some use for them.

Here is a script for importing the headerr file for you for good measure (from 1475), I believe that you from Snow Leopard onwards then can write name: stdio.h for instance, in the Spotligt bar. :wink:

do shell script "mdimport /usr/include
mdimport /usr/local/include
mdimport /System/Library/Frameworks"

And while I am at it, here are two snippets for turning Airport on and off:

do shell script "networksetup -setairportpower en1 on"

I knew you’d guess it but:

do shell script "networksetup -setairportpower en1 off"

:slight_smile:

Hello.

Here are some links to some quicklook plugins that makes the script above work as they used to before Mavericks.

sindresorhus/quick-look-plugins

QuickLook Plugins List

There is is also an AppleScript Quick look plugin out there that works, as an aside. The one I use is named ScriptQL.qlgenerator

The path to the header files are now changed to:

(Thanks to Shane Stanley.)

This is for the mdimport you’ll probably have to do manually by the commands in the post right above.

Hello.

Should you have the need to debug quicklook, as it stops to work, then start looking for plugins that are in either /Library/QuickLook and ~/Library/QuickLook, and has a modification date back to 2007 . :wink:

Hello.

This is script that installs a git repo for an Xcode 5 project when either you are upgrading the project to Xcode 5, or forgot/refused to install a git repo in the first place.

You should stand on the root folder of the project in a Finder window, and execute the script from there. The folder structure of the Xcode project should be willy nilly, with a folder within the root folder of the project that carries the same name.

The script is kindof a state machine. It works as it should, but may be hard to modify.

You may want to modify it slightly if you are running Xcode 3 or Xcode 4 alongside Xcode 5.

global theLoc

set situ to 1
installGitRepo(situ)
on installGitRepo(situ)
	global theLoc
	tell application "Finder"
		try
			if situ = 1 then
				set theSel to selection
				if theSel is {} then error number 3000
				set theKind to class of item 1 of theSel
				if theKind is not folder then error number 3000
				set didSelect to false
			else
				set theSel to {}
				set end of theSel to choose folder default location theLoc with prompt "Select Xcode root folder."
				set didSelect to true
			end if
			
			set theName to name of item 1 of theSel
			if not didSelect and not (exists folder theName of (item 1 of theSel)) then error number 3001
			set pxPath to quoted form of POSIX path of (item 1 of theSel as alias)
			set repoExists to (do shell script "cd " & pxPath & "; test -d .git && echo \"true\" || echo \"false\"") as boolean
			if repoExists then
				display alert "A git repository is already in place"
				if situ = 2 then set target of Finder window 1 to item 1 of theSel
				set situ to -1
			else
				--every thing so far is okay time to create the git repo.
				set pxPath to quoted form of POSIX path of (item 1 of theSel as alias)
				
				do shell script "cd " & pxPath & " ; mkdir .git ; cat \"UserInterfaceState.xcuserstate
build
*.pbxuser
*.perspectivev3
*.mode1v3
*~
*~.nib
*~.xib 
.DS_Store 
xcuserdata/\" >.git/.gitignore;git init; git add .; git commit -m \"initial commit\""
				display alert "Successfully created .git repo for Xcode Project " & theName
				if situ = 2 then set target of Finder window 1 to item 1 of theSel
				set situ to 0
			end if
		on error e number n
			if n = -128 then
				set situ to -3
			else if n < 3000 then
				display alert "Unrecoverable error: 
" & e
				set situ to -2
			else if n = 3000 then
				set theTarget to name of its target of Finder window 1 -- as alias as text
				display alert "No folder selected in " & theTarget
				set situ to 2
			else if n = 3001 then
				set theFolder to name of item 1 of theSel
				display alert "No folder of folder " & theFolder & " named " & theFolder & ".
The folder can't be the root folder of an Xcode project."
				set situ to 2
			end if
		end try
		if situ > 0 then set theLoc to target of Finder window 1 as alias
	end tell
	
	if situ > 0 then
		installGitRepo(situ)
	else
		if situ = 0 then
			set theLoc to missing value
			if running of application "Xcode" is true then
				tell application "Finder" to display alert "You must restart Xcode to make it acknowledge the repo."
			else
				tell application "Finder" to display alert "You can start Xcode to use source control."
			end if
		else if situ = -1 then
			tell application "Finder" to display dialog "Do you want to create a repo for another folder?" with icon note
			set situ to 1
			installGitRepo(situ)
		end if
	end if
end installGitRepo


Hello.

I updated the script above, so that if the folder structure deviates from having subfolder with the same name, then you are allowed to use the same root folder, whence you select the folder the second time around, which you are forced to.

I am a fan of this script, since it lets me play with other peoples examples/code, without fear of not being able to go back to the original, and also without the hassle of having several copies of the same project, and the hurdles of figuring out which is which.