[TxMt] save file AFTER command has been run?

Jim Bagrow bagrowjp at gmail.com
Sun Jan 29 17:44:48 UTC 2006


Hi Guys,

First time poster, been using TM for a while though.

I'm trying to write a command that will run, do it's thing, then save the
file.  The trick is how to get it to save AFTER the command alters the
file.  Is this possible?

Specifically, what I am trying to do is update a `Last Modified:` string
whenever I save a file.  That's the easy part, I did it with  some python
triggered by cmd-s.  Now that the string has been altered, how do I save the
file?  It seems possible with applescript, but I can't get it to work.

If anyone has any suggestions about this final step, that would be great.  I
know there's something wrong with my applescript, it tends to behave
differently in osascript than in Script Editor, is this because of the
escaped quotes?  Something to do with combining triple- and single-quotes?

I've searched everywhere, apple documentation, textmate wiki/mailing list,
and turned up nothing!  It seems to me that having a "Save After" option for
Commands would be useful.  What do you guys think? Maybe this has been
discussed already.

Thanks for your help,
Jim Bagrow

Here is the command:
Save:  Current File
Command:
*************************************************************
#!/usr/bin/env python

import os
import re
import datetime

# get selected text from shell:
path = os.getenv('TM_FILEPATH')
f = open(path, 'r')
text = f.read()
f.close()

string = "Last Modified: "+datetime.date.isoformat( datetime.datetime.today()
)
p = re.compile('Last Modified: \d\d\d\d-\d\d-\d\d')
result = p.sub(string, text)

print result

# how can I save the file at this point, AFTER it's been modified?
# PS: I hate applescript!

cmd = """osascript -e \"
set thePath to do shell script \"echo $TM_FILEPATH\"

tell application \"TextMate\"
      activate
     save current document in thePath
end tell
\"
"""

os.popen(cmd)
*************************************************************
Input: Entire Document
Output: Replace Document
Activation: Key Equivalent -> "cmd-S"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macromates.com/textmate/attachments/20060129/69eb9bcb/attachment.html>


More information about the textmate mailing list