Sublime Forum

Open In Sublime (finder toolbar app for OS X)

#1

Open in sublime is a little finder toolbar app that will open the current folder in the sidebar of Sublime Text 2.


Grab it from the repo on github.

0 Likes

#2

I like the idea, but it looks as though the path to subl in main.scpt is a hardcoded path specific to your machine. I was able to fix it by simply changing the path to the location within Sublime Text 2.app:

do shell script "/Applications/Sublime\\ Text\\ 2.app/Contents/SharedSupport/bin/subl " & "'" & currentPath & "'"
0 Likes

#3

[quote=“jon4than”]I like the idea, but it looks as though the path to subl in main.scpt is a hardcoded path specific to your machine. I was able to fix it by simply changing the path to the location within Sublime Text 2.app:

do shell script "/Applications/Sublime\\ Text\\ 2.app/Contents/SharedSupport/bin/subl " & "'" & currentPath & "'"

i can’t believe i’m such an idiot.

thanks for the fix. github repo updated with fixed version of the app.

fwiw, i tried to come up with a way to let applescript find the path to Sublime Text 2 (in case user keeps it somewhere other than /Applications) and use that path to find the way to subl, but for some reason even when applescript had the path right, the do shell script command would bomb telling me that there was no such file

set st2 to POSIX path of (path to application "Sublime Text 2")
set subl to st2 & "Contents/SharedSupport/bin/subl "

do shell script quoted form of subl & "'" & currentPath & "'"

i could see in the “results” box in the applescript editor that it was getting the path to subl right, but it never worked. so for now, thanks to you jon4than, hard coded to at least the right place.

0 Likes

#4

Looks nice! I’m one of those troublemakers who keeps his apps organized instead of all in one folder, but I’m capable of editing the file to work on my machine.

Thanks for sharing!

0 Likes

#5

Can’t you just drag the file to the Sublime icon in the Dock? That’s what I do, for files and folders.

0 Likes

#6

You can also drag the app itself to the toolbar and then drag files or folders on it to open them.

0 Likes

#7

Thanks for the script! I hadn’t even thought about this yet, it’s sure to make my work a bit more fluent.

I modified your script a bit to work on the current selection and fallback on the current folder if no file is selected, this way I can use it to open files or folders I have selected as well. I would have forked your script on Github and requested a pull, but it seems you haven’t actually committed the code on github, so here are the modifications:

[code]-- script was opened by click in toolbar
on run

tell application "Finder"
	copy selection to theSelected
	set outputPathList to {}
	repeat with anItem in theSelected
		copy (POSIX path of (anItem as alias)) to end of outputPathList
	end repeat
	set AppleScript's text item delimiters to return
	set currentPath to outputPathList as string
	set AppleScript's text item delimiters to ""
	if currentPath is equal to "" then
		set currentPath to (POSIX path of (target of front window as string))
	end if
	
	tell current application to do shell script "/Applications/Sublime\\ Text\\ 2.app/Contents/SharedSupport/bin/subl " & "'" & currentPath & "'"
end tell

end run
[/code]

I don’t know much about Applescript so I pretty much cut and paste this together from online resources, I’m sure that someone who has played around with Applescript more than I have can come up with a cleaner version, but this does the job.

0 Likes

#8

@handycam, @minimalweb: yes, you can do both of those things, the only thing that this little app adds in terms of convenience is the ability to do a one-click open of the current directory that is open in a finder window. if you want to drag a folder onto the ST2 icon to open it up, your finder window has to be showing you the parent of the directory you are interested in. i find that i often am looking at the contents of the directory that i want to work in, so it is convenient to have one-click ST2 access to that directory. it’s not a big deal, i just like it and thought others might too.

@Naatan: i didn’t commit the code because the app doesn’t run as just a script and it seemed like a lot of extra work for people to open it up in applescript, save it as an app, copy paste the icon, etc. for such a simple little one-trick pony. but i will pull out the main script from the app and commit that. i thought about doing what you did in terms of opening the current selection, but then i realized that it was already easy to do that for files with a right-click “open with” from the finder and all i really wanted was for ST2 to open the current finder window folder. but i’m glad you made it work for you.

update: acchh, never mind. the main.scpt is binary too. there’s really no code worth committing to the repo, i don’t think.

i’m going to keep trying to find a solution that lets me dynamically find the way to the subl inside ST2 so that people who keep it somewhere other than /Applications could also use this without having to re-compile it. if i figure out how to make that work, i’ll post back here and update the repo. but for anyone who wants to modify the behavior and/or location of subl, you can just open the app up in the applescript editor and make it do as you like.

0 Likes

#9

ok, so i finally figured out how to find subl dynamically. i’ve updated the version of the app at github. here is the code that is in there, fwiw:

-- script was opened by click in toolbar
on run
	set st2 to POSIX path of (path to application "Sublime Text 2")
	set subl to st2 & "Contents/SharedSupport/bin/subl"
	
	tell application "Finder"
		set currentPath to (POSIX path of (target of front window as string))
		tell current application to do shell script "\"" & subl & "\"" & " " & "'" & currentPath & "'"
	end tell
	
end run
0 Likes

#10

I see you’ve committed it on github as a txt, just a suggestion, if you rename it to code.applescript (or “open in sublime.applescript” for that matter), people can just double click it and save it as an app for easy compilation.

0 Likes

#11

In that case, you can drag the proxy icon of the folder in the Finder window titlebar to your dock icon:


0 Likes

#12

thanks. done and i added a little readme too.

0 Likes

#13

Here’s my version that integrates Naatan’s changes (and fixes a bug when you select multiple files to open) with automatic location of the app bundle. This one seems to do exactly what I want, except for one thing – I wish there was a way to make it so option-clicking would open a new window for the file(s) instead of opening them in tabs of an existing window. But my applescript-fu is very weak…

-- script was opened by click in toolbar
on run
    set st2 to POSIX path of (path to application "Sublime Text 2")
    set subl to st2 & "Contents/SharedSupport/bin/subl"
    
    tell application "Finder"
        copy selection to theSelected
        set outputPathList to {}
        repeat with anItem in theSelected
            copy "'" & ((POSIX path of (anItem as alias)) as string) & "'" to end of outputPathList
        end repeat
        set AppleScript's text item delimiters to " "
        set currentPath to outputPathList as string
        set AppleScript's text item delimiters to ""
        if currentPath is equal to "" then
            set currentPath to "'" & (POSIX path of (target of front window as string)) & "'"
        end if
        tell current application to do shell script "'" & subl & "' " & currentPath
    end tell
    
end run
0 Likes

#14

i bow down. thanks for that cool tip.

i still like the 1-click finder window toolbar applet better than a click and drag though.

also, for whatever reason, if you put ST2 in the toolbar and drag from the proxy icon to it, it looks to me like it only adds an alias to the sidebar in ST2 that doesn’t resolve to the directory you want to see.

0 Likes

#15

[quote=“n8gray”]Here’s my version that integrates Naatan’s changes (and fixes a bug when you select multiple files to open) with automatic location of the app bundle. This one seems to do exactly what I want, except for one thing – I wish there was a way to make it so option-clicking would open a new window for the file(s) instead of opening them in tabs of an existing window. But my applescript-fu is very weak…
[/quote]

my applescript-fu is also very weak and not likely to get much better i’m afraid because it has got to be one of the most opaque, unwieldy languages i have ever messed with.

subl will open a new window if you include -n in the command line and here is a little thread i found that talks about a maybe cumbersome way to figure out if the applescript is launched with the option key down. YMMV.

0 Likes

#16

On a related note, you can also easily create a service to open the selected files or folders in Sublime Text.

Using Automator, create a new service and configure it like this:

https://img.skitch.com/20120223-tng91qc2p717gpge6cqhj45jxj.png

Save as, for instance, Open in Sublime Text 2. Done.

To make it even more convenient, you can assign a keyboard shortcut to the service in System Preferences / Keyboard.

0 Likes

#17

Version 2.0 of Open In Sublime is available on github.

Version 2 is a complete re-write with some new features.

  1. It works with Path Finder as well as Finder, and it also works better with Total Finder

  2. By popular demand and the work of ProLoser, this verison of Open in Sublime lets it open the finder selection in Sublime Text 2. You can have either the original behavior or the open selection behavior depending on how you name the application that you create in AppleScript. If you name the app “open folder in sublime” then you will get the original behavior which is to always load the open finder (path finder) window in Sublime’s sidebar regardless of whether there is a selection in the window or not. If you name it anything else, then you will get the new behavior which is to open the selection if there is one and the directory if there is not.

0 Likes

#18

[quote=“svenax”]On a related note, you can also easily create a service to open the selected files or folders in Sublime Text.

Using Automator, create a new service and configure it like this:

https://img.skitch.com/20120223-tng91qc2p717gpge6cqhj45jxj.png

Save as, for instance, Open in Sublime Text 2. Done.

To make it even more convenient, you can assign a keyboard shortcut to the service in System Preferences / Keyboard.[/quote]

I really like this. Although the command line should be:

open -a "Sublime Text 2" --args -n $@

Will continue to work, even if the internal binary moves. And when Sublime Text 3 comes out, you can just remove the " 2" part.

0 Likes

#19

thank you !! works great !!

0 Likes