Scripts for Smile, AS and SD to open hardcoded path in current window

Hello.

Name: OpenAsLibrary

This is a preliminary release, the scripts are to be merged as one and grossly enhanced during the week.
with the option of opening the folder instead of the file, if the control key is held down.
It will also open posix paths, which they don’t right now.

Usage:
You just put the cursor anywhere on the line which contains the hardcoded library path or selects it if you please, and the run this script. Then the library opens up in a new window.

Recommended shortcut keys (will work in all AppleScript Editors) is cmd opt O for opening the file, when the final version arrives you should put the open the folder option on cmd opt ctrl O

I will at some time not so distant in the future support both AppleMods and ScriptFactory’s loaders with this.

Enjoy :smiley:

AppleScript Editor 2.x


-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
” macscripter.net/viewtopic.php?pid=macscripter.net/viewtopic.php?pid=131743#p131743
(*
TERMS OF USE. 
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 

You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.
*)
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131743#p131743 for reference and terms of use.
set HYPHEN to "\""
set ESCAPE to "\\"

set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}

tell application "AppleScript Editor"
	tell its document 1
		activate
		set startPoint to its character range of selection
		copy startPoint to probe
		check syntax
		set its selection to characters ((item 1 of startPoint) as integer) thru ((item 2 of startPoint) as integer)
		
		
		set {origStart, origEnd} to {(item 1 of probe), (item 2 of probe)}
		set {selStart, selEnd} to {(item 1 of probe), (item 2 of probe)}
		if (selStart - selEnd) is not equal to 0 then
			set aFileName to its characters ((item 1 of startPoint) as integer) thru ((item 2 of startPoint) as integer) as Unicode text
			set aFileName to my cleanFileName(aFileName)
			
			if my couldOpenAsFile(aFileName) then
				set AppleScript's text item delimiters to tids
				return
			end if
			
			set selEnd to selStart + 1
		else
			set selEnd to selEnd + 1
		end if
		set fWardList to {}
		set bWardList to {}
		
		repeat
			if character selStart is return then exit repeat
			if character selStart is HYPHEN then set end of fWardList to selStart
			set selStart to selStart - 1
			if selStart is equal to 0 then exit repeat
		end repeat
		set fWardList to reverse of fWardList
		repeat
			if character selEnd is return then exit repeat
			if character selEnd is HYPHEN then set end of bWardList to selEnd
			set selEnd to selEnd + 1
		end repeat
		
		set hyphensList to fWardList & bWardList
		repeat with i from 1 to ((count hyphensList) div 2) by 2
			set aFileName to characters ((item i of hyphensList) as integer) thru ((item (i + 1) of hyphensList) as integer) as Unicode text
			set aFileName to my cleanFileName(aFileName)
			if my couldOpenAsFile(aFileName) then
				set AppleScript's text item delimiters to tids
				return
			end if
		end repeat
	end tell
end tell
set AppleScript's text item delimiters to tids


on couldOpenAsFile(aFileName)
	
	if (count aFileName) is not equal to 0 then
		try
			aFileName as alias
			tell application "AppleScript Editor" to open aFileName
			tell its document 1 to activate
			return true
		on error
			return false
		end try
	end if
end couldOpenAsFile


on cleanFileName(aFileName)
	-- removes everything that shouldn't be there when we try to create a fileName
	repeat
		
		if text item 1 of aFileName is my ESCAPE or text item 1 of aFileName is my HYPHEN then
			set aFileName to text items 2 thru -1 of aFileName as Unicode text
		else
			exit repeat
		end if
		if (count aFileName) is not equal to 0 then exit repeat
	end repeat
	
	if (count aFileName) is not equal to 0 then
		repeat
			
			if text item -1 of aFileName is my ESCAPE or text item -1 of aFileName is my HYPHEN then
				set aFileName to text items 1 thru -2 of aFileName as Unicode text
			else
				exit repeat
			end if
			if (count aFileName) is 0 then exit repeat
		end repeat
	end if
	return aFileName
end cleanFileName


Script Debugger 4.5


-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
” macscripter.net/viewtopic.php?pid=macscripter.net/viewtopic.php?pid=131743#p131743
(*
TERMS OF USE. 
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 

You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.
*)
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131743#p131743 for reference and terms of use.
property test : ""
set HYPHEN to "\""
set ESCAPE to "\\"

set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}

tell application "Script Debugger 4.5"
	tell its script window 2
		activate
		set startPoint to character range of selection of its document
		if item 1 of startPoint > item 2 of startPoint and item 2 of startPoint ≠ 0 then
			set probe to item 2 of startPoint
			set item 2 of startPoint to item 1 of startPoint
			set item 1 of startPoint to probe
		end if
		copy startPoint to probe
		set endPos to ((item 1 of startPoint) as integer) + ((item 2 of startPoint) as integer)
		tell its document to compile
		set test to script source of its document
		if item 2 of probe is not 0 then
			set item 2 of probe to (item 2 of probe) - (item 1 of probe) + 1
		end if
		set selection of its document to probe --  as list
		
		set {origStart, origEnd} to {(item 1 of startPoint), (item 2 of startPoint)}
		set {selStart, selEnd} to {(item 1 of probe), (item 2 of probe)}
		if (selStart - selEnd) is not equal to 0 and selEnd is not equal to 0 then
			set aFileName to (characters ((item 1 of startPoint) as integer) thru ((item 2 of startPoint) as integer) of test) as Unicode text
					set aFileName to my cleanFileName(aFileName)
			
			if my couldOpenAsFile(aFileName) then
				set AppleScript's text item delimiters to tids
				return
			end if
			set selEnd to selStart + 1
		else
			set selEnd to selStart + 1
		end if
		set fWardList to {}
		set bWardList to {}
		if character selStart of test is return then set selStart to selStart - 1
		repeat
			if character selStart of test is return then exit repeat
			if character selStart of test is HYPHEN then set end of fWardList to selStart
			set selStart to selStart - 1
			if selStart is equal to 0 then exit repeat
		end repeat
		set fWardList to reverse of fWardList
		repeat
			if character selEnd of test is return then exit repeat
			if character selEnd of test is HYPHEN then set end of bWardList to selEnd
			set selEnd to selEnd + 1
		end repeat
		
		set hyphensList to fWardList & bWardList
		repeat with i from 1 to ((count hyphensList) div 2) by 2
			set aFileName to (characters ((item i of hyphensList) as integer) thru ((item (i + 1) of hyphensList) as integer) of test) as Unicode text
			set aFileName to my cleanFileName(aFileName)
			if my couldOpenAsFile(aFileName) then
				set AppleScript's text item delimiters to tids
				return
			end if
		end repeat
	end tell
end tell
set AppleScript's text item delimiters to tids


on couldOpenAsFile(aFileName)
	
	if (count aFileName) is not equal to 0 then
		try
			aFileName as alias
			tell application "Script Debugger 4.5" to open aFileName
			tell its document 1 to activate
			return true
		on error
			return false
		end try
	end if
end couldOpenAsFile


on cleanFileName(aFileName)
	-- removes everything that shouldn't be there when we try to create a fileName
	repeat
		
		if text item 1 of aFileName is my ESCAPE or text item 1 of aFileName is my HYPHEN then
			set aFileName to text items 2 thru -1 of aFileName as Unicode text
		else
			exit repeat
		end if
		if (count aFileName) is not equal to 0 then exit repeat
	end repeat
	
	if (count aFileName) is not equal to 0 then
		repeat
			
			if text item -1 of aFileName is my ESCAPE or text item -1 of aFileName is my HYPHEN then
				set aFileName to text items 1 thru -2 of aFileName as Unicode text
			else
				exit repeat
			end if
			if (count aFileName) is 0 then exit repeat
		end repeat
	end if
	return aFileName
end cleanFileName

Smile


-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
” macscripter.net/viewtopic.php?pid=macscripter.net/viewtopic.php?pid=131743#p131743
(*
TERMS OF USE. 
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 

You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.
*)
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131743#p131743 for reference and terms of use.
property test : ""
set HYPHEN to "\""
set ESCAPE to "\\"

set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}

tell application "Smile"
	tell its window 1
		activate
		
		set startPoint to selection
		copy startPoint to probe
		check syntax
		set test to characters ((item 1 of startPoint) as integer) thru ((item 2 of startPoint) as integer) of its text
		set selection to startPoint
		
		set {origStart, origEnd} to {(item 1 of probe), (item 2 of probe)}
		set {selStart, selEnd} to {(item 1 of probe), (item 2 of probe)}
		if (selStart - selEnd) is not equal to 0 then
			set aFileName to its characters ((item 1 of startPoint) as integer) thru ((item 2 of startPoint) as integer) as Unicode text
			set aFileName to my cleanFileName(aFileName)
			
			if my couldOpenAsFile(aFileName) then
				set AppleScript's text item delimiters to tids
				return
			end if
			set selEnd to selStart + 1
		else
			set selEnd to selEnd + 1
		end if
		set fWardList to {}
		set bWardList to {}
		
		repeat
			if character selStart is return then exit repeat
			if character selStart is HYPHEN then set end of fWardList to selStart
			
			set selStart to selStart - 1
			if selStart is equal to 0 then exit repeat
		end repeat
		set fWardList to reverse of fWardList
		repeat
			if character selEnd is return then exit repeat
			if character selEnd is HYPHEN then set end of bWardList to selEnd
			
			set selEnd to selEnd + 1
		end repeat
		
		set hyphensList to fWardList & bWardList
		repeat with i from 1 to ((count hyphensList) div 2) by 2
			set aFileName to characters ((item i of hyphensList) as integer) thru ((item (i + 1) of hyphensList) as integer) as Unicode text
			set aFileName to my cleanFileName(aFileName)
			if my couldOpenAsFile(aFileName) then
				set AppleScript's text item delimiters to tids
				return
			end if
		end repeat
	end tell
end tell
set AppleScript's text item delimiters to tids


on couldOpenAsFile(aFileName)
	
	if (count aFileName) is not equal to 0 then
		try
			aFileName as alias
			tell application "Smile" to open aFileName
			tell its document 1 to activate
			return true
		on error
			return false
		end try
	end if
end couldOpenAsFile


on cleanFileName(aFileName)
	-- removes everything that shouldn't be there when we try to create a fileName
	repeat
		
		if text item 1 of aFileName is my ESCAPE or text item 1 of aFileName is my HYPHEN then
			set aFileName to text items 2 thru -1 of aFileName as Unicode text
		else
			exit repeat
		end if
		if (count aFileName) is not equal to 0 then exit repeat
	end repeat
	
	if (count aFileName) is not equal to 0 then
		repeat
			
			if text item -1 of aFileName is my ESCAPE or text item -1 of aFileName is my HYPHEN then
				set aFileName to text items 1 thru -2 of aFileName as Unicode text
			else
				exit repeat
			end if
			if (count aFileName) is 0 then exit repeat
		end repeat
	end if
	return aFileName
end cleanFileName


Hello.

I forgot to mention in the post above, that is cool to put the AppleScript Editor Version in its context menu.
You stand on the line, then invokes the context menu if you are mouse centric at the moment, and voila your library file is the fronmost window :).

Hello.

I have now merged the three separate versions into one, which are easier for me to update and maintain.
This script works from Smile, AppleScript Editor and ScriptDebugger 4.5
I have added some more functionality as well. If you press down control while executing the script, or having a second shortcut key to it which includes the control key, then you will open a finder window which shows the library file.
A library file can be specified with either a posix path including the “~” operator, or an hfs path,

You need Stefan’s checkModifierKeys when the script is run. And you need to change the absolute path of it in the script to reflect your placement of it.

I intend to modify the script later on, as it has become a little bit more sluggish than I thought. It is on the other hand far faster than you in doing what it accomplishes.

Recommended short cut keys are still cmd opt O for normal behaviour, and cmd ctrl opt O for opening the folder.

Enjoy! :smiley:


-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- macscripter.net/viewtopic.php?pid=131438#p131438
(*
TERMS OF USE. 
This applies only to posting code, as long as you don't post it on other websites, or displays it (the code) on your own, you are welcome to do
whatever you want to do with it without any further permission. 
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code. 
I don't require you to keep this whole novel, but you must keep the link which refers to where it was originally posted. And my copyright.
You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribute AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code when you distribute as read only.

Credit for having contributed to your product would in all cases be nice!

If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.

The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.

I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code. 

The above paragraphs are also valid if you violate any of my terms.

If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.

*)
-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131438#p131438 for reference and terms of use.
property inited : missing value
property HYPHEN : "\"" -- properties needed for parsing the files
property ESCAPE : "\\"
property McUsr : "Version 1.02 macscripter.net/viewtopic.php?pid=131438#p131438"
on run
	
	if my inited is missing value then
		set my nativeSupport's supportedAppBundleIds to {"com.satimage.Smile", "com.apple.ScriptEditor2", "com.latenightsw.ScriptDebugger"}
		my nativeSupport's setInstalledAppsBundeleIds(my nativeSupport's findInstalledApps())
		set my nativeSupport's handlerList to {"fileAliasForFrontDocument", "aLibraryPathFromFrontDocument"}
		set my nativeSupport's nativeHandlers to {{my fileAliasForFrontDocument_com_satimage_Smile, my fileAliasForFrontDocument_com_apple_ScriptEditor2, my fileAliasForFrontDocument_com_latenightsw_ScriptDebugger}, {my aLibraryPathFromFrontDocument_com_satimage_Smile, my aLibraryPathFromFrontDocument_com_apple_ScriptEditor2, my aLibraryPathFromFrontDocument_com_latenightsw_ScriptDebugger}}
		
		set my inited to true
	end if
	
	set cntrlWasPressed to false -- **
	
	-- ==========================================================================
	if ((do shell script "/usr/local/opt/checkModifierKeys control") is "1") then
		-- *** you will have to edit this path to where you will store or has stored checkModifierKeys 
		set cntrlWasPressed to true
	end if
	-- ==========================================================================
	set itIsOkToTryToOpenLibrary to false
	
	set bidFrontApp to my bundleId's frontmostBundleId()
	if my nativeSupport's frontmostAppAmongstInstalled(bidFrontApp) then
		set itIsOkToTryToOpenLibrary to true
	else if frontmostAppAmongstSupported(bidFrontApp) then
		tell me
			activate
			try
				display dialog "The fronmost application is currently not supported. 
			Open the script and enter a space somewhere before saving it.
			Then run the script again."
			end try
		end tell
		
	end if
	
	-- A fall back pattern, first current file, then buffer list, then choose file to list
	if itIsOkToTryToOpenLibrary is true then
		
		set thisScript to my nativeSupport's dispatcher_0_(1, bidFrontApp)
		-- this lines returns an alias to the current file if it is a file from a document
		-- in one of the three editors, false otherwise.
		if thisScript is not false then
			set scriptToOpen to my nativeSupport's dispatcher_0_(2, bidFrontApp)
			
			if scriptToOpen is not null then
				if cntrlWasPressed is false then
					if item 1 of scriptToOpen is not false then
						my openAsFile(item 2 of scriptToOpen, bidFrontApp)
					else
						my showFirstExistingParentFolderInFinder(item 2 of my scriptToOpen)
						my inform("Couldn't open the file
I have revealed the closed existing folder in Finder.")
					end if
				else
					if item 1 of scriptToOpen is not false then
						tell application "Finder"
							reveal file (item 2 of scriptToOpen)
							tell its Finder window 1
								set index of it to 1
							end tell
						end tell
					else
						my showFirstExistingParentFolderInFinder(item 2 of my scriptToOpen)
						my inform("The file didn't exist.
I have opened the closest existing parent folder in Finder.")
					end if
				end if
			end if
			
		end if
	end if
end run


-- © McUsr and Put in public domain 2010. You may use it as you will but not post it elsewhere. as stated above.
-- Find some other post of mine in code exchange and read what that implies.
script nativeSupport
	-- LLCurrentFile has more complete, but still more primitive.
	property installedAppsBundleIds : {} -- should be hard coded as well?
	property supportedAppBundleIds : {}
	-- should be hardcoded when the installer is written.
	--	property assumedDefaultApp : "com.apple.ScriptEditor2"
	--	property LSRoleId : "com.apple.applescript.script"
	property nativeHandlers : {}
	property handlerList : {}
	on setInstalledAppsBundeleIds(listOfUTId)
		set my nativeSupport's installedAppsBundleIds to listOfUTId
	end setInstalledAppsBundeleIds
	
	on frontmostAppAmongstInstalled(thisBundleId)
		-- returns true if frontmost app is among installed.
		if thisBundleId is in my nativeSupport's installedAppsBundleIds then
			return true
		else
			return false
		end if
	end frontmostAppAmongstInstalled
	
	on frontmostAppAmongstSupported(thisBundleId)
		-- returns true if frontmost app is among supported.
		if my bundleId's frontmostBundleId() is in my nativeSupport's supportedAppBundleIds then
			return true
		else
			return false
		end if
	end frontmostAppAmongstSupported
	
	
	on dispatcher_0_(functIdx, UTI)
		-- 0 because it takes 1 parameter! every handler must return a result
		global _McUsrs_nativeFuncToDispatch
		local theRes, uti_index
		set uti_index to my nativeSupport's IndexOfItem(UTI, my nativeSupport's installedAppsBundleIds)
		set _McUsrs_nativeFuncToDispatch to item uti_index of item functIdx of my nativeSupport's nativeHandlers
		set theRes to _McUsrs_nativeFuncToDispatch()
		return theRes
	end dispatcher_0_
	
	on IndexOfItem(theItem, theList) -- credit to Emmanuel Levy but I modified it with the considering case statements
		considering case
			set text item delimiters to return
			set theList to return & theList & return
			set text item delimiters to {""}
			try
				-1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
			on error
				0
			end try
		end considering
	end IndexOfItem
	
	on findInstalledApps()
		-- returns the installed apps on the users machine
		script o
			property l : {}
		end script
		repeat with aBundleId in my nativeSupport's supportedAppBundleIds
			try
				tell application "Finder"
					set a to name of application file id aBundleId
					set end of o's l to contents of aBundleId
				end tell
			end try
		end repeat
		return o's l
	end findInstalledApps
	
end script

-- © McUsr and Put in public domain 2010. You may use it as you will but not post it elsewhere. as stated above.

script bundleId
	on defaultAppBundleId(LSHandlerContentTypeOrTagClass) -- Made considerably shorter and faster by Nigel Garvey
		-- Not LSHandlerContentTag check out defaults read com.apple.LaunchServices |more
		-- Doesn't work awfully well with public.filename-extension!
		try
			set LSHandlerRoleAll to (do shell script "/usr/bin/defaults read com.apple.LaunchServices | /usr/bin/grep -A 2 " & LSHandlerContentTypeOrTagClass)
			set {tids, text item delimiters} to {text item delimiters, "\""}
			set LSHandlerRoleAll to text item -2 of LSHandlerRoleAll
			set text item delimiters to tids
			return LSHandlerRoleAll
		on error
			return "" -- The app is most probably the assumed one.
		end try
	end defaultAppBundleId
	
	on frontmostBundleId()
		return id of application (path to frontmost application as Unicode text)
	end frontmostBundleId
end script

(* ======== * fileAliasForFrontDocument * ======== *)
on fileAliasForFrontDocument_com_satimage_Smile()
	local thisScript, s
	using terms from application "Smile"
		set s to application id "com.satimage.Smile"
		tell s
			if (count its script windows) is greater than 0 then
				set thisScript to path name of its front window as alias
				return thisScript
			else
				return false
			end if
		end tell
	end using terms from
end fileAliasForFrontDocument_com_satimage_Smile


on fileAliasForFrontDocument_com_latenightsw_ScriptDebugger()
	local thisScript, s
	using terms from application "Script Debugger 4.5"
		set s to application id "com.latenightsw.ScriptDebugger"
		tell s
			if (count its documents) is greater than 0 then
				set thisScript to file spec of its document of its script window 1 as alias
				return thisScript
			else
				return false
			end if
		end tell
	end using terms from
end fileAliasForFrontDocument_com_latenightsw_ScriptDebugger

on fileAliasForFrontDocument_com_apple_ScriptEditor2()
	
	local thisScript
	tell application id "com.apple.ScriptEditor2"
		if (count its documents) is greater than 0 then
			set thisScript to POSIX file (path of its front document) as alias -- as Unicode text
			return thisScript
		else
			return false
		end if
	end tell
end fileAliasForFrontDocument_com_apple_ScriptEditor2

on aLibraryPathFromFrontDocument_com_apple_ScriptEditor2()
	local startPoint, probe, aF
	tell application "AppleScript Editor"
		tell its document 1
			activate
			set startPoint to its character range of selection
			if item 1 of startPoint > item 2 of startPoint and item 2 of startPoint ≠ 0 then
				set probe to item 2 of startPoint
				set item 2 of startPoint to item 1 of startPoint
				set item 1 of startPoint to probe
			end if
			
			copy startPoint to probe
			check syntax
			set its selection to characters ((item 1 of startPoint) as integer) thru ((item 2 of startPoint) as integer)
			set aF to my aLibraryPathFromFrontDocument_commonHandler(it, probe, startPoint)
			return aF
		end tell
	end tell
end aLibraryPathFromFrontDocument_com_apple_ScriptEditor2

on aLibraryPathFromFrontDocument_com_latenightsw_ScriptDebugger()
	local tids, startPoint, probe, scriptText, origStart, origEnd, selstart, selEnd, aFileName, fwardlist, bWardList, hyphensList
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
	
	using terms from application "Script Debugger 4.5"
		set s to application id "com.latenightsw.ScriptDebugger"
		tell s
			
			tell its script window 1
				activate
				
				set startPoint to character range of selection of its document
				if item 1 of startPoint > item 2 of startPoint and item 2 of startPoint ≠ 0 then
					set probe to item 2 of startPoint
					set item 2 of startPoint to item 1 of startPoint
					set item 1 of startPoint to probe
				end if
				copy startPoint to probe
				tell its document to compile
				set scriptText to script source of its document
				if item 2 of probe is not 0 then
					set item 2 of probe to (item 2 of probe) - (item 1 of probe) + 1
				end if
				set selection of its document to probe --  as list
				
				set {origStart, origEnd} to {(item 1 of startPoint), (item 2 of startPoint)}
				set {selstart, selEnd} to {(item 1 of probe), (item 2 of probe)}
				if (selstart - selEnd) is not equal to 0 and selEnd is not equal to 0 then
					set selEnd to selstart + 1
				else
					set selEnd to selstart + 1
				end if
				
				if character selstart of scriptText is return then set selstart to selstart - 1
				repeat
					if character selstart of scriptText is return or selstart is equal to 0 then
						return null
					else if character selstart of scriptText is my HYPHEN then
						set fwardHyphen to selstart
						exit repeat
					end if
					set selstart to selstart - 1
					
				end repeat
				
				repeat
					if character selEnd of scriptText is return then
						return null
					else if character selEnd of scriptText is HYPHEN then
						set bWardHyphen to selEnd
						exit repeat
					end if
					set selEnd to selEnd + 1
				end repeat
				
				
				set aFileName to (characters fwardHyphen thru bWardHyphen of scriptText) as Unicode text
				-- we do convert to a hfs filename here. if it was a posix
				set aFileName to my cleanFileName(aFileName)
				try
					aFileName as alias
					set AppleScript's text item delimiters to tids
					return {true, aFileName}
				end try
				set AppleScript's text item delimiters to tids
				return {false, aFileName}
			end tell
		end tell
	end using terms from
end aLibraryPathFromFrontDocument_com_latenightsw_ScriptDebugger


on aLibraryPathFromFrontDocument_com_satimage_Smile()
	local startPoint, probe, aF
	using terms from application "Smile"
		set s to application id "com.satimage.Smile"
		tell s
			tell its window 1
				activate
				
				set startPoint to selection
				if item 1 of startPoint > item 2 of startPoint and item 2 of startPoint ≠ 0 then
					set probe to item 2 of startPoint
					set item 2 of startPoint to item 1 of startPoint
					set item 1 of startPoint to probe
				end if
				
				copy startPoint to probe
				check syntax
				-- 		set test to characters ((item 1 of startPoint) as integer) thru ((item 2 of startPoint) as integer) of its text
				set selection to startPoint
				set aF to my aLibraryPathFromFrontDocument_commonHandler(it, probe, startPoint)
				return aF
			end tell
		end tell
	end using terms from
end aLibraryPathFromFrontDocument_com_satimage_Smile


on aLibraryPathFromFrontDocument_commonHandler(aRefToTextWindow, probe, startPoint)
	-- returns null if didn't find anything to try to open at all.
	-- returns false if there was something to try to open, but it couldn't be found
	-- returns hfsFilename if there indeed was something to open
	local tids, origStart, origEnd, selstart, selEnd, aFileName, fwardHyphen, bWardHyphen
	
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
	tell aRefToTextWindow
		
		set {origStart, origEnd} to {(item 1 of probe), (item 2 of probe)}
		set {selstart, selEnd} to {(item 1 of probe), (item 2 of probe)}
		if (selstart - selEnd) is not equal to 0 then
			
			set selEnd to selstart + 1
		else
			set selEnd to selEnd + 1
		end if
		
		repeat
			if character selstart is return or selstart is equal to 0 then
				return null
			else if character selstart is my HYPHEN then
				set fwardHyphen to selstart
				exit repeat
			end if
			set selstart to selstart - 1
			
		end repeat
		
		repeat
			if character selEnd is return then
				return null
			else if character selEnd is my HYPHEN then
				set bWardHyphen to selEnd
				exit repeat
			end if
			set selEnd to selEnd + 1
		end repeat
		
		set aFileName to characters fwardHyphen thru bWardHyphen as Unicode text
		-- we do convert to a hfs filename here. if it was a posix
		set aFileName to my cleanFileName(aFileName)
		try
			aFileName as alias
			set AppleScript's text item delimiters to tids
			return {true, aFileName}
		end try
		set AppleScript's text item delimiters to tids
		return {false, aFileName}
	end tell
end aLibraryPathFromFrontDocument_commonHandler


on openAsFile(aFileName, idBundle)
	
	if (count aFileName) is not equal to 0 then
		try
			aFileName as alias
			tell application id idBundle to open aFileName
			tell its document 1 to activate
			return true
		on error e number n
			my inform(e & " : " & n)
			
			return false
		end try
	end if
end openAsFile


on cleanFileName(aFileName)
	-- removes everything that shouldn't be there when we try to create a fileName
	repeat
		
		if text item 1 of aFileName is my ESCAPE or text item 1 of aFileName is my HYPHEN then
			set aFileName to text items 2 thru -1 of aFileName as Unicode text
		else
			exit repeat
		end if
		if (count aFileName) is not equal to 0 then
			exit repeat
		else
			return ""
		end if
	end repeat
	
	if (count aFileName) is not equal to 0 then
		repeat
			
			if text item -1 of aFileName is my ESCAPE or text item -1 of aFileName is my HYPHEN then
				set aFileName to text items 1 thru -2 of aFileName as Unicode text
			else
				exit repeat
			end if
			if (count aFileName) is not equal to 0 then
				exit repeat
			else
				return ""
			end if
		end repeat
	end if
	set aFileName to my hfsPathOfAnythingAsText(aFileName)
	return aFileName
end cleanFileName

-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131797#p131797 for reference and terms of use.
on hfsPathOfAnythingAsText(anyFileOrFolderPath)
	-- returns the full hfs pathname of anything if the files exists
	local tids, theFile, lastItem, tidsToUse, singleQuoteCheck -- Thanks to Yvan Koenig :)
	set singleQuoteCheck to false
	set tidsToUse to ":"
	try
		(get class of anyFileOrFolderPath)
	on error number -1728 -- it was a filereference
		set fileOrFolderPath to fileOrFolderPath as alias as text
		return fileOrFolderPath
	end try
	
	set anyFileOrFolderPath to "" & anyFileOrFolderPath -- doesn't harm.
	if anyFileOrFolderPath starts with "'" and anyFileOrFolderPath ends with "'" then
		set anyFileOrFolderPath to text 2 thru -2 of anyFileOrFolderPath
		set singleQuoteCheck to true
	end if
	if anyFileOrFolderPath does not start with "/" and anyFileOrFolderPath does not start with "~" then
		return anyFileOrFolderPath -- we had a hfspath
	else
		set tidsToUse to "/"
	end if
	
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
	set anyFileOrFolderPath to text items of anyFileOrFolderPath as text
	if singleQuoteCheck is true then
		set AppleScript's text item delimiters to "'\\''"
		set anyFileOrFolderPath to text items of anyFileOrFolderPath -- as list
		set AppleScript's text item delimiters to "'"
	end if
	-- if tidstouse was "/" then we must add the disk name, - but which ???
	set anyFileOrFolderPath to "" & anyFileOrFolderPath
	set AppleScript's text item delimiters to tidsToUse
	if text item 1 of anyFileOrFolderPath is "~" then
		set anyFileOrFolderPath to {item 1 of (list disks)} & text items 2 thru -2 of (POSIX path of (path to home folder) as text) & text items 2 thru -1 of anyFileOrFolderPath
	else if text item 2 of anyFileOrFolderPath is "Volumes" then
		set anyFileOrFolderPath to text items 3 thru -1 of anyFileOrFolderPath
	else
		set anyFileOrFolderPath to {item 1 of (list disks)} & text items 2 thru -1 of anyFileOrFolderPath
	end if
	set AppleScript's text item delimiters to ":"
	set anyFileOrFolderPath to "" & anyFileOrFolderPath
	set AppleScript's text item delimiters to tids
	return anyFileOrFolderPath
end hfsPathOfAnythingAsText


-- © McUsr 2010 and put in Public Domain see: macscripter.net/viewtopic.php?pid=131797#p131797 for reference and terms of use.
on getPathToParentFolderOfAnythingAsText(anyFileOrFolderPath)
	-- whether it is a file, a folder,application or bundle.
	local tids, theFile, lastItem, tidsToUse, singleQuoteCheck
	set singleQuoteCheck to false
	try
		(get class of anyFileOrFolderPath)
	on error number -1728 -- it was a filereference
		set anyFileOrFolderPath to anyFileOrFolderPath as alias
	end try
	set anyFileOrFolderPath to "" & anyFileOrFolderPath -- doesn't harm.
	if anyFileOrFolderPath starts with "'" and anyFileOrFolderPath ends with "'" then
		set anyFileOrFolderPath to text 2 thru -2 of anyFileOrFolderPath
		set singleQuoteCheck to true
	end if
	set tidsToUse to ":"
	if anyFileOrFolderPath starts with "/" or anyFileOrFolderPath starts with "~" then set tidsToUse to "/"
	set lastItem to -2
	if anyFileOrFolderPath ends with tidsToUse then set lastItem to -3
	-- we gain an extra text item  the empty one created at the end.
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
	set anyFileOrFolderPath to text items 1 thru lastItem of anyFileOrFolderPath
	set anyFileOrFolderPath to "" & anyFileOrFolderPath
	if singleQuoteCheck is true then
		set AppleScript's text item delimiters to "'\\''"
		set anyFileOrFolderPath to text items of anyFileOrFolderPath
		set AppleScript's text item delimiters to "'"
	end if
	set anyFileOrFolderPath to "" & anyFileOrFolderPath -- necessary here.
	set AppleScript's text item delimiters to tids
	return anyFileOrFolderPath
end getPathToParentFolderOfAnythingAsText

on showFirstExistingParentFolderInFinder(parentFolder)
	-- Show closest existing folder in finder
	local foundParentFolder
	
	set foundParentFolder to false
	repeat until foundParentFolder is true or foundParentFolder is "" -- sjekk parentFolder for dette!
		set parentFolder to my getPathToParentFolderOfAnythingAsText(parentFolder)
		try
			(parentFolder & ":") as alias
			set foundParentFolder to true
		end try
	end repeat
	if parentFolder is not "" then
		tell application "Finder"
			launch
			open folder parentFolder
			tell its Finder window 1
				set index of it to 1
			end tell
		end tell
	end if
end showFirstExistingParentFolderInFinder

on inform(theMessage)
	local libraryIcon
	set libraryIcon to a reference to file "Macintosh HD:System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:ToolbarLibraryFolderIcon.icns"
	tell me
		activate
		display dialog theMessage with title "OpenAsLibrary" buttons {"Ok"} default button 1 with icon libraryIcon
	end tell
end inform

Hello.

I have “optimized” it. What I have done, is speeded up the parsing of what is between hyphens. There is no longer any gain in selecting the file name between hyphens and thereby short circuiting the algorithm. This was to expensive when there were no direct hit, for the rest of the parsing. You must now keep the cursor somewhere between the two hyphens. The “any where on the line” attempt was too expensive to.

This is as fast as this gets for my part. I’m moving all my common parts over onto a script server I will continue to use, since this will make it easier to maintain, and the script will become faster to run since parts of it might be already loaded into the servers memory.

What remains is to try to resolve some parent folder, when the actual file wasn’t found. It might not be any filename at all between the hyphens. -This is to be done before this is considered finished.

Hello.

I have made it to open the closes’t existing folder, if said library script file doesn’t exist, whether you want it to open normally or reveal it in finder.

There is one more thing to be done, and that is to ensure that it works under Finder, I will do that in a batch one day, as this is something related to several scripts. I might also manage to create localized messages.

There is a lack of sound too.

The updated script can be found in post #3