Miscellanous scripts

Hello.

This is a script that helps you locate and view a C-header file, the syntax is gheader stdio, and not gheader stdio.h, it is meant to work with Apple’s headers, the standard C-library, and what libraries you have downloaded via Macports. (/opt/local/include). You’ll get the hang of how to modify it, should you need it.

The shell script, should easily be incorporated into an applescript, and leverages upon the searchpaths I have (you specify ) specified. Also the fact that the include file with the shortest path is the most correct one. So if you need a different searchpath, then you’ll have to add some if clauses. It is also quite silent if you execute it from an applescript, only echoing the path of the header file if you are in a terminal window.

[code]#!/bin/bash

http://macscripter.net/viewtopic.php?pid=162319#p162319

© 2013 McUsr and put in public domain you may not post this elsewhere.

if [ $# -eq 0 ]; then
if test -t 0 ; then
echo “gheader: You need to supply a name that resembles a header file as a parameter.” >/dev/tty
fi
exit 1;
fi
toopen=0
if [ $1 = -o ] ; then
toopen=1
shift
fi
if [ $# -eq 0 ]; then
if test -t 0 ; then
echo “gheader: You need to supply a name that resembles a header file as a parameter.” >/dev/tty
fi
exit 1;
fi
header=${1%%.h}.h
a=$(locate $header |grep "^/System/Library/./$header" |awk ‘{ print length $1 }’ |sort |sed -n '1s/([[:digit:][[:digit:]])([^[:digit:]][^[:digit:]])/\2/p’)
if test -z $a ; then
a=$(locate $1 |grep "^/usr/include/.
/$header" |awk ‘{ print length $1 }’ |sort |sed -n '1s/([[:digit:][[:digit:]])([^[:digit:]][^[:digit:]])/\2/p’)
if test -z $a ; then
a=$(locate $1 |grep "^/opt/local/include/.
/$header" |awk ‘{ print length $1 }’ |sort |sed -n '1s/([[:digit:][[:digit:]])([^[:digit:]][^[:digit:]]*)/\2/p’)
if test -z $a ; then
if test -t 0 ; then
echo “gheader: Couldn’t locate $header” >/dev/tty
fi
exit 1
fi
fi
fi
if test -t 0 ; then
echo $a >/dev/tty
fi
if [ $toopen -eq 0 ]; then
qlmanage -px $a 2>&1 >/dev/null &
else
open $a &
fi
exit 0[/code]

Hello.

I have evolved the script in post #1 to be agnostic to whether you supplied a .h extension or not.

Hello.

I have changed the script in post #1 to provide a better form of agnosticisim. :wink:

Trouble was, with the previous scheme, that .so files and what not, ended up with an icon in quicklook, so I have inverted it all, looking strictly for header files, and adding any missing .h suffixes for starters instead of ignoring them during the lookups.

Hello.

I have updated the script in post #1, to be sure that the latest version is there, I have renamed it to ghead for brevity.

I call it with the script below, to have the header files I want to have a peek at handy, from a history list. This concept isn’t fully evolved yet, one day I care, and have the time, I’ll synchronize this history with some other scripts I use from Safari, Preview and XCode, to have one common history list file.

-- http://macscripter.net/viewtopic.php?pid=162319#p162319
-- © McUsr and put in public domain you may not post this elsewhere.


property headerFile : ""
property histList : {"No Header files entered."}
global executableIcon
on run
	local fromclip, thNm
	set {fromclip, thNm} to {false, ""}
	tell application id "sevs"
		if UI elements enabled is not true then
			set UI elements enabled to true
		end if
		set thNm to name of item 1 of (every application process whose visible is true and frontmost is true)
	end tell
	tell application thNm to activate
	if thNm is in {"TextWrangler", "BBEdit", "XCode", "Preview", "Safari", "TextEdit"} then
		set theSpare to the clipboard as record
		set the clipboard to ""
		tell application thNm to activate
		if thNm is not "Safari" then
			tell application id "sevs" to keystroke "c" using command down
			set extraction to the clipboard as text
		else
			tell application "Safari"
				activate
				do JavaScript "self.focus()"
				set extraction to (do JavaScript "\"\"+window.getSelection();" in its document 1)
				if extraction = "" then
					tell application id "sevs" to keystroke "c" using command down
					set extraction to the clipboard
				end if
			end tell
		end if
		set the clipboard to theSpare as record
		if extraction ≠ "" then
			set headerFile to extraction
			set fromclip to true
		end if
	else
		tell application thNm to activate
	end if
	
	if not fromclip then
		set executableIcon to a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:ExecutableBinaryIcon.icns")
		try
			tell application "SystemUIServer"
				activate
				
				set {headerFile, tbutton} to {text returned, button returned} of (display dialog "Enter Header file" with title "Header Files" default answer headerFile buttons {"History", "Cancel", "Ok"} default button 3 with icon executableIcon)
			end tell
		on error e number n
			tell application thNm to activate
			tell application id "sevs"
				click window 1 of application process thNm
			end tell
			error number -128
		end try
		tell application thNm to activate
		if tbutton = "History" then
			tell application "SystemUIServer"
				activate
				set headerFileFromList to choose from list histList default items item 1 of histList with title "Header Files"
			end tell
			if headerFileFromList = false then
				tell application "SystemUIServer"
					activate
					try
						display dialog "Do you want to enter one after all?" with title "Header Files" with icon executableIcon
						set headerFile to text returned of (display dialog "Enter Header file" with title "Header Files" default answer headerFile with icon executableIcon)
					on error e number nu
						tell application thNm to activate
						tell application id "sevs"
							click window 1 of application process thNm
						end tell
						error number -128
					end try
				end tell
			else
				set headerFile to headerFileFromList as text
				if headerFile = "No Header files entered." then
					tell application thNm to activate
					tell application id "sevs"
						click window 1 of application process thNm
					end tell
					error number -128
				end if
			end if
		end if
	end if
	set res to (do shell script "ghead  " & headerFile & " 2>&1 >/dev/null ; test $? -eq 0 && echo \"true\" || echo \"false\"") as boolean
	if res = true then
		if "No Header files entered." is in histList then
			set histList to rest of histList
		end if
		if headerFile is not in histList then
			set end of histList to headerFile
		end if
	end if
	tell me to quit
end run

Hello.

I thought: “why not coalesce all the scripts into one, then I don’t have to write handlers for writing the history for each and every script that uses ghead in post #1”. The script in the post above, is a “one size fits all”, that is to be used from every app, with and without a selection. You have to see to that nothing is selected in order to get the dialog box, but it respects the previous contents of the clip board.

I have also fixed an issue whith Safari, when it gets to be obnoxious, and don’t let me copy the selection of the front window.

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