I have the following drag command for zip files to be inserted as snippet:
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:"$TM_DROPPED_FILE"}">${3:"$TM_DROPPED_FILE"}</a>"
This will insert a download snippet for a default URL on a live server.
However, I would like to strip the path somehow from the variable.
Does anyone know how to do this,through a substitution reg ex maybe ?
Additionally a variable named $TM_FILE_NAME (That would just insert the name of the file instead of $TM_DROPPED_FILE would make much sense here for such a drag command, where you don't care so much about the path.
regards, marios
marios wrote:
I have the following drag command for zip files to be inserted as snippet:
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:"$TM_DROPPED_FILE"}">${3:"$TM_DROPPED_FILE"}</a>"
This will insert a download snippet for a default URL on a live server.
However, I would like to strip the path somehow from the variable.
Does anyone know how to do this,through a substitution reg ex maybe ?
ruby can do this for you:
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:`ruby -e 'print ENV['''TM_DROPPED_FILE'''].gsub(/^(.*/)/, String.new)'`}">${3:$2}</a>"
Torsten Becker wrote:
marios wrote:
I have the following drag command for zip files to be inserted as snippet:
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:"$TM_DROPPED_FILE"}">${3:"$TM_DROPPED_FILE"}</a>"
This will insert a download snippet for a default URL on a live server.
However, I would like to strip the path somehow from the variable.
Does anyone know how to do this,through a substitution reg ex maybe ?
ruby can do this for you:
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:`ruby -e 'print ENV['''TM_DROPPED_FILE'''].gsub(/^(.*/)/, String.new)'`}">${3:$2}</a>"
I tried the above, but String.new outputs nil instead. (My SupportFolder is updated through subversion)
regards, marios
marios wrote:
I tried the above, but String.new outputs nil instead. (My SupportFolder is updated through subversion)
regards, marios
Hmm, I did check it again (with Tiger's ruby) and I just get nil in the output if I don't have everything in one line, please try to put the above in just one line and it should work. (Take care that you don't accidently remove a \ or so .. they are important :>)
I also attached the snippet maybe you want to use this instead of copy and pasting. :)
Torsten Becker wrote:
marios wrote:
I tried the above, but String.new outputs nil instead. (My SupportFolder is updated through subversion)
regards, marios
Hmm, I did check it again (with Tiger's ruby) and I just get nil in the output if I don't have everything in one line, please try to put the above in just one line and it should work. (Take care that you don't accidently remove a \ or so .. they are important :>)
I also attached the snippet maybe you want to use this instead of copy and pasting. :)
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
Yes, that works. There where three newline characters, in my code sample. One more useful keyboard shortcut, that I need to remember in all those cases:
option command I (Show invisibles )
Your code sample works as expected. Thanks many. I am happy. I also took a look, at Alan's Drag Example that uses Perl instead, as part of the Documentation, but as he mentioned : Slightly complex.
regards, marios
Hi, again.
I had a look again today at the drag commands documentation (man sec 11)
" TM_MODIFIER_FLAGS — the modifier keys which were held down when the file got dropped. This is a bitwise OR in the form: SHIFT|CONTROL|OPTION|COMMAND (in case all modifiers were down). "
However, I couldnt' figure this one out.
What I would like to do , is to make the command conditional, so it will do a different thing with the URL holding option and command.
How do I change the code, so it uses a different Variable in these cases ? (for instance, in this case, using a different variable like $TM_LOCAL_URL instead of $TM_REMOTE_URL)
This is the current drag command:
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:`ruby -e 'print ENV['''TM_DROPPED_FILE'''].gsub(/^(.*/)/,String.new)'`}">${3:$2}</a>"
Also , I tried to modify the code so it strips off the file-extension from the third variable doing the usual TM Transformation, but then I get an Error , gsub complaining.
Thanks again, regards, marios
On 30/9/2006, at 22:38, marios wrote:
[...] How do I change the code, so it uses a different Variable in these cases ? (for instance, in this case, using a different variable like $TM_LOCAL_URL instead of $TM_REMOTE_URL)
You can do something like:
if [[ "$TM_MODIFIER_FLAGS" = *OPTION* ]] then BASE="$TM_LOCAL_URL" else BASE="$TM_REMOTE_URL" fi
Then in your command use "$BASE" (which will be the local URL when ⌥ is down).
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:`ruby -e 'print ENV['''TM_DROPPED_FILE'''].gsub(/^(.*/)/,String.new)'`}">${3:\ $2}</a>"
Also , I tried to modify the code so it strips off the file-extension from the third variable doing the usual TM Transformation, but then I get an Error , gsub complaining.
Just paste an example of the desired result.
Allan Odgaard wrote:
On 30/9/2006, at 22:38, marios wrote:
[...] How do I change the code, so it uses a different Variable in these cases ? (for instance, in this case, using a different variable like $TM_LOCAL_URL instead of $TM_REMOTE_URL)
You can do something like:
if [[ "$TM_MODIFIER_FLAGS" = *OPTION* ]] then BASE="$TM_LOCAL_URL" else BASE="$TM_REMOTE_URL" fi
Then in your command use "$BASE" (which will be the local URL when ⌥ is down).
Wonderful. I've got this working now, except, there is one peculiar thing happening, that's not directly related with this. When I use option, and drag the File, The document window looses focus and disappears under the cursor, when the file gets dropped in the document window, so I changed the whole thing to use Shift instead, where I get the expected behavior. This is what I have so far using your recommendation:
if [[ "$TM_MODIFIER_FLAGS" = *SHIFT* ]] then BASE="$TM_LOCAL_URL" else BASE="$TM_REMOTE_URL" fi echo -n "<a href="http://%24BASE/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:`ruby -e 'print ENV['''TM_DROPPED_FILE'''].gsub(/^(.*/)/,String.new)'`}">${3:$2}</a>"
echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:`ruby -e 'print ENV['''TM_DROPPED_FILE'''].gsub(/^(.*/)/,String.new)'`}">${3:$2}</a>"
Also , I tried to modify the code so it strips off the file-extension from the third variable doing the usual TM Transformation, but then I get an Error , gsub complaining.
Just paste an example of the desired result.
This is the output that I have using the above modification.
<a href="http://www.consking.com/file_download/file_id" title="test.abc">test.abc</a> <a href="http://www.local.dev/file_download/file_id" title="test.abc">test.abc</a>
What I am trying to achieve is this:
<a href="http://www.consking.com/file_download/file_id" title="test">test</a> <a href="http://www.local.dev/file_download/file_id" title="test">test</a>
,where test is a transformation of $TM_DROPPED_FILE getting stripped off both the relative path AND the file extension .abc
I had a look also, at http://macromates.com/blog/archives/2005/09/26/shell-variables/, Is there a shorthand way, also to strip both, at the end and the beginning, using this method ?
regards, marios
On 1/10/2006, at 14:26, marios wrote:
[...] When I use option, and drag the File, The document window looses focus and disappears under the cursor, when the file gets dropped in the document window, so I changed the whole thing to use Shift instead, where I get the expected behavior.
What’s happening here is, that if you ⌥-click another application, your active application will hide. So you need to first drag the icon, then press ⌥.
[...] This is the output that I have using the above modification.
<a href="http://www.consking.com/file_download/file_id" title="test.abc">test.abc</a> <a href="http://www.local.dev/file_download/file_id" title="test.abc">test.abc</a>
What I am trying to achieve is this:
<a href="http://www.consking.com/file_download/file_id" title="test">test</a> <a href="http://www.local.dev/file_download/file_id" title="test">test</a>
,where test is a transformation of $TM_DROPPED_FILE getting stripped off both the relative path AND the file extension .abc
I had a look also, at http://macromates.com/blog/archives/2005/09/26/shell-variables/, Is there a shorthand way, also to strip both, at the end and the beginning, using this method ?
The easiest is to use the regexp substitution capabilities of TextMate snippets.
So use something like ${TM_DROPPED_FILE/.*/|..+//g} — that ought to strip both the leading directories and the trailing extension.
Allan Odgaard wrote:
On 1/10/2006, at 14:26, marios wrote:
[...] When I use option, and drag the File, The document window looses focus and disappears under the cursor, when the file gets dropped in the document window, so I changed the whole thing to use Shift instead, where I get the expected behavior.
What’s happening here is, that if you ⌥-click another application, your active application will hide. So you need to first drag the icon, then press ⌥.
Ok, I see. thanks a lot.
The easiest is to use the regexp substitution capabilities of TextMate snippets.
So use something like ${TM_DROPPED_FILE/.*/|..+//g} — that ought to strip both the leading directories and the trailing extension.
I still couldn't get the above going, I tried to escape also the above variable and if I do echo the above snippet, as a drag command causes a crash. Shouldn't really get my fingers on this too much, since it is not possible to show the output as a tooltip, tends to be a little dangerous.
If I just wanted to echo the filename and strip all the rest off, so I can reuse it and store it in a variable, what would be the best approach ?
regards, marios
marios wrote:
Allan Odgaard wrote:
On 1/10/2006, at 14:26, marios wrote:
[...] When I use option, and drag the File, The document window looses focus and disappears under the cursor, when the file gets dropped in the document window, so I changed the whole thing to use Shift instead, where I get the expected behavior.
What’s happening here is, that if you ⌥-click another application, your active application will hide. So you need to first drag the icon, then press ⌥.
Ok, I see. thanks a lot.
The easiest is to use the regexp substitution capabilities of TextMate snippets.
So use something like ${TM_DROPPED_FILE/.*/|..+//g} — that ought to strip both the leading directories and the trailing extension.
Sorry,again
Lets say, in the above example leaving Thorsten Beckers excellent Ruby solution in place, in order to strip off hte file exension, would one need to expand $TM_DROPPED_FILE first again, before the pattern match and replacement take place ?
I tried to play around with it in UNIX scope, to avoid typos, but no luck.
regards, marios
On 5. Oct 2006, at 05:19, marios wrote:
The easiest is to use the regexp substitution capabilities of TextMate snippets.
So use something like ${TM_DROPPED_FILE/.*/|..+//g} — that ought to strip both the leading directories and the trailing extension.
I still couldn't get the above going, I tried to escape also the above variable and if I do echo the above snippet, as a drag command causes a crash.
Please paste the code you tried, and certainly also the one which lead to a crash.
I finally could figure out how to do this, without using the Ruby code, thanks to Allan's excellent documentations.:
base=${TM_DROPPED_FILE%.*} title="${base##*/}" echo -n "<a href="http://%24TM_REMOTE_URL/file_download/%5C%24%7B1:file_id%7D%5C" title="${2:$title}">${3:$title}</a>"
However, in order to work this correctly, the file must be dragged slowly. If I drag the file to fast, then the regions for the tab stop variables 1, 2 and 3 will loose track and cease to function correctly, since they are highlighted at unproper positions.
The command is attached.
regards, marios
Regarding to the previous drag command:
In order for this to work correctly, freehand editing mode must not be enabled while dragging. (Thanks Allan) Then it works right out of the box, even while dragging fast.
However, I wanted to have the command, so it also strips off double and triple file extensions for the title attribute, as often the case with archive files, changing the code a little in the substitution pattern. I inserted also an additional tab stop value for the URL and added a fall-back value(www.exampe.com) in case $TM_REMOTE_URL is unset. A newline is also inserted after the last tab-trigger.
base="${TM_DROPPED_FILE/%.*/}" title="${base##*/}" url2="www.example.com" url1="${TM_REMOTE_URL:-$url2}" echo -n "<a href="http://%5C%24%7B1:%24url1%7D/file_download/%5C%24%7B2:file_id%7D%5C" title="${3:$title}">${4:$title}</a> "
Command is attached.
I was thinking, that it would be terrific, to make those commands, so the can do bulk inserting as well. May be that would be possible, by setting a counter for the tab-stop variables so they increment automatically for each bulk dropped file.
regards, marios
I had forgotten to include the modifier key flags. Also to make this command a little bit more useful, I added a Filetype variable that can be used, to add some extra text behind the title. It uses shift to distinguish between the base Urls, which when not set up will default to example.com.
base="${TM_DROPPED_FILE/%.*/}" title="${base##*/}" FILETYPE=" Download" url2="www.example.com" url1="${TM_REMOTE_URL:-$url2}" url="${TM_LOCAL_URL:-$url2}" if [[ "$TM_MODIFIER_FLAGS" = *SHIFT* ]] then BASE="$url" else BASE="$url1" fi echo -n "<a href="http://%5C%24%7B1:%24BASE%7D/file_download/%5C%24%7B2:file_id%7D%5C" title="${3:$title}$FILETYPE">${4:$title}$FILETYPE</a> "
I'm trying to figure out, how it can be done, so it does bulk inserting as well, in spite the fact, that they have tab stop variables, but still no go. (May be with sed ?)
regards, marios
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
marios wrote:
I had forgotten to include the modifier key flags. Also to make this command a little bit more useful, I added a Filetype variable that can be used, to add some extra text behind the title. It uses shift to distinguish between the base Urls, which when not set up will default to example.com.
base="${TM_DROPPED_FILE/%.*/}" title="${base##*/}" FILETYPE=" Download" url2="www.example.com" url1="${TM_REMOTE_URL:-$url2}" url="${TM_LOCAL_URL:-$url2}" if [[ "$TM_MODIFIER_FLAGS" = *SHIFT* ]] then BASE="$url" else BASE="$url1" fi echo -n "<a href="http://%5C%24%7B1:%24BASE%7D/file_download/%5C%24%7B2:file_id%7D%5C" title="${3:$title}$FILETYPE">${4:$title}$FILETYPE</a> "
I'm trying to figure out, how it can be done, so it does bulk inserting as well, in spite the fact, that they have tab stop variables, but still no go.
Tried some strange constructs with expressions, or let n=(n+1) with sed, but that doesn't seem to give any valid syntax. The reason, why I am so much after this, is that it would open new roads not only for the drag commands, but other commands as well.
regards, marios
On 13. Oct 2006, at 23:46, marios wrote:
I'm trying to figure out, how it can be done, so it does bulk inserting as well, in spite the fact, that they have tab stop variables, but still no go.
Tried some strange constructs with expressions, or let n=(n+1) with sed, but that doesn't seem to give any valid syntax [...]
When you drop multiple files on TextMate, it will iteratively run the drag command (i.e. once for each dropped file).
There is a few workarounds for this, like testing a modifier and going into “batch mode”, but nothing pretty.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Allan Odgaard wrote:
On 13. Oct 2006, at 23:46, marios wrote:
I'm trying to figure out, how it can be done, so it does bulk inserting as well, in spite the fact, that they have tab stop variables, but still no go.
Tried some strange constructs with expressions, or let n=(n+1) with sed, but that doesn't seem to give any valid syntax [...]
When you drop multiple files on TextMate, it will iteratively run the drag command (i.e. once for each dropped file).
There is a few workarounds for this, like testing a modifier and going into “batch mode”, but nothing pretty.
Thanks for the tip, I might give this a try, however it's not top priority.
regards, marios