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
|