Home Download Buy Blog Forum Support

Variable for $Home Directory?

Variable for $Home Directory?

Postby SunBob on Tue Jul 02, 2013 11:12 am

Hi,

I just was wondering if there is a variable like $packages, which stores the path of the installed packages, for the "Home"-directory in ST2.

Sadly, something like this won't work:
Code: Select all
[
    { "caption": "Open TODO", "command": "open_file", "args": {"file": "${HOME}/Documents/Privat/privat.TODO"} }
or
    { "caption": "Open TODO", "command": "open_file", "args": {"file": "~/Documents/Privat/privat.TODO"} }
]


As you can see, I want to open a specific file using the ctrl+shift+p command.

Has anybody some hints for me, how it should look like?
SunBob
 
Posts: 3
Joined: Fri Oct 19, 2012 1:37 pm

Re: Variable for $Home Directory?

Postby skuroda on Tue Jul 02, 2013 11:58 pm

You could do something like this (http://stackoverflow.com/questions/1599 ... 1#16408331). Of course, that answer is specifically for expanding variables, so replace the for loop with something like

Code: Select all
s = sublime.load_settings(filename)
for key in setting_keys:
    value = s.get(key)
    value = os.path.expandvars(value)
    value = os.path.expanduser(value)
    s.set(key, value)


Untested, but should work.
skuroda
 
Posts: 677
Joined: Fri Oct 05, 2012 8:11 am

Re: Variable for $Home Directory?

Postby SunBob on Wed Jul 10, 2013 3:45 pm

Hi skuroda,

first thx for your reply.

I have to admit, that I am not that familiar with this kind of coding.
Does your suggestion mean that I have to write my own plugin to get the variable which stores the path to the home-directory? :S

Just to clarify, I am syncing my settings of Sublime between MacOS and Windows, and want to open a file which is located in the users document folder. The problem of course is, that the path to this directory is different in both OS and so I can't use my setting on both OS. Or is there a way to use different *.sublime-commands-files for different OS? ;)
SunBob
 
Posts: 3
Joined: Fri Oct 19, 2012 1:37 pm

Re: Variable for $Home Directory?

Postby skuroda on Thu Jul 11, 2013 6:45 am

Yes you have to write a plugin. But if you check the stackoverflow link, the plugin is already written. Just replace the contents of the for loop with what I posted before. "~" is expanded properly in both windows and os x. To demonstrate that to yourself, enter the following into the ST console.

Code: Select all
import os


Code: Select all
os.path.expanduser("~")


This method would require you to place the files in the same location on both machines (relative to the home directory). I don't know if you can create a OS specific *.sublime-command file.
skuroda
 
Posts: 677
Joined: Fri Oct 05, 2012 8:11 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 19 guests