Previous  |  Main  |  Next
AppleScript Logo

Open Files of Path List

Script for: BBEdit
Copyright: Matthias Steffens, use at your own risk!
Description: If one or more file paths (e.g. obtained by the "Open Special" script)
are selected in the front window of BBEdit, this script will open the
corresponding files in BBEdit or within their default application.
Necessary: None
Download: OpenFilesOfPathList_v1.2.hqx (for BBEdit 6.x)
OpenFilesOfPathList_v1.1.hqx (for BBEdit 5.x)
History: v1.2  -  works now with BBEdit 6.x (but not with BBEdit 5.x anymore -- use v1.1 together with BBEdit 5.x)
v1.1  -  code was re-written completely :). The following enhancements were made:
  • removed requirement for "ACME Script Widgets 2.5" scripting addition
  • the formerly chosen button will now be the default button upon next run
  • now only one routine will serve to open the files wether they'll be opened by BBEdit or by the Finder
v1.0  -  original script posted at this site
The Script:
property SelectedButton : "BBEdit"
global NoOfPaths, thePaths, FilesNotFound, ItemNo

set FilesNotFound to 0
set ItemNo to 1
set thePaths to {}
set Die to false

tell application "BBEdit 6.0"
	activate
	if not (exists text window 1) then -- BBEdit 5.x: "if not (exists window 1) then"
		display dialog "No open document!" with icon caution buttons "Oops" default button 1
		set Die to true
	else
		set TheItems to selection of text window 1 as string -- BBEdit 5.x: "set TheItems to selected text"
	end if
end tell

if not Die then
	if TheItems is "" then -- nothing selected
		display dialog "You have to select one or more file or folder paths in order to use this " & ¬
			"script!" with icon caution buttons "OK" default button 1
	else
		-- split paragraphs:
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {return}
		set TheItems to text items of TheItems
		repeat with theItem in TheItems
			if theItem as string is not "" then
				copy theItem as string to end of thePaths
			end if
		end repeat
		set AppleScript's text item delimiters to oldDelims
		-- end split paragraphs
		
		-- instead of the "split paragraphs"-code above you might want to use the "ACME Script Widgets 2.5"
		-- scripting addition, which handles the problem within one line:
		-- set thePaths to tokenize TheItems with delimiters {return} without null tokens -- uses ACME Script Widgets 2.5
		
		set NoOfPaths to count thePaths
		if NoOfPaths = 1 then
			set Plural_S to ""
			set Plural2 to "its"
		else
			set Plural_S to "s"
			set Plural2 to "their"
		end if
		set dialogResult to display dialog "Open the " & NoOfPaths & " item" & Plural_S & " in BBEdit or within " & Plural2 & " default " & ¬
			"application?" with icon note buttons {"Cancel", "Default Application", "BBEdit"} default button SelectedButton
		if button returned of dialogResult is not "Cancel" then
			set SelectedButton to button returned of dialogResult
			if SelectedButton is "Default Application" then
				OpenFiles(application "Finder")
			end if
			if SelectedButton is "BBEdit" then
				tell application "BBEdit 6.0"
					set win_ct to count text windows -- BBEdit 5.x: "set win_ct to count windows"
					if win_ct > 0 then
						-- The following dialog will only appear when there are open windows in BBEdit:
						set dialogResult to display dialog "There are open windows in BBEdit." & return & "Close these" & ¬
							" windows (with save dialog if necessary) before opening the item" & Plural_S & return & ¬
							"found?" with icon caution buttons {"Cancel", "Yes", "No"} default button 3
						if button returned of dialogResult is "Cancel" then
							error number -128
						else if button returned of dialogResult is "Yes" then
							close every window
						end if
					end if
				end tell
				my OpenFiles(application "BBEdit 6.0")
			end if
			if FilesNotFound = 1 then
				display dialog "" & FilesNotFound & " item wasn't found!" with icon caution buttons "OK" default button 1
			else if FilesNotFound > 1 then
				display dialog "" & FilesNotFound & " items were not found!" with icon caution buttons "OK" default button 1
			end if
		end if
	end if
end if


on OpenFiles(PreferredApp)
	tell PreferredApp
		try
			repeat with i from ItemNo to NoOfPaths
				open item i of thePaths as alias
				set ItemNo to ItemNo + 1
			end repeat
		on error
			set FilesNotFound to FilesNotFound + 1
			set ItemNo to ItemNo + 1
			my OpenFiles(PreferredApp)
		end try
	end tell
end OpenFiles

 


Contact: Matthias Steffens  |  Previous  |  Main  |  Next  |  Last Updated: 24-Sep-00