|
|
#1 |
|
Major Leaguer
Join Date: May 2002
Location: atl, ga, usa
Posts: 344
|
Services: Make New Text File Here
I find I need to make plain text files often, as README files to remind me what a folder is for, and for hundreds of other reasons. I've already set TextEdit to make plain text by default.
I also tend to favor the keyboard over the mouse, mainly because it's faster for me, but also to save wear and tear on my computer-worn wrists and hands. What I find myself doing, over & over, is going to the Services menu [while using almost any app] and choosing TextEdit, then one of the options [Open Selected File, or New Window Containing Selection]. I do this just to get a blank TextEdit document. What I'd really like to see is an item on the Services menu, "New TextEdit Document Here". That way, whether or not TextEdit were open, I could quickly generate a plain text file. The "Here" part comes in to play whenever my active window is a Finder window, in which case TextEdit should assume the file will be saved in that same folder, and all I should have to do is type Apple-S, type a filename, and hit Return -- the file should be saved in that topmost Finder window. What do you think? I'm open to suggestions here about an even easier/quicker way to create TE plaintext documents from the keyboard. I tried setting a system-wide keyboard shortcut, but I can't seem to get that to work. Last edited by osxpounder; 03-04-2004 at 03:09 PM. |
|
|
|
|
|
#2 |
|
MVP
Join Date: Dec 2003
Location: Redlands, CA
Posts: 2,300
|
Here is another area where PC excels. The right click of a PC provides a "New" contextual menu where you can make a new text file, new folder etc.
Sadly missing from the Mac is a way to customize two-button mice menu options.
__________________
Tour Israel and Jordan via CD-ROM |
|
|
|
|
|
#3 |
|
Triple-A Player
Join Date: May 2003
Posts: 150
|
You could use an AppleScript like the one below, and attach it to a keystroke with an application like AnotherLauncher/Butler, iKey, Drag Thing, XKeys, etc.
Code:
tell application "Finder" to set save_folder to (target of window 1) as alias tell application (path to frontmost application as Unicode text) set text_file to choose file name with prompt "Enter file name:" default location save_folder end tell set file_ref to open for access text_file with write permission set eof file_ref to 0 close access file_ref tell application "Finder" set file type of (text_file as alias) to "TEXT" set creator type of (text_file as alias) to "ttxt" end tell tell application "TextEdit" activate open text_file end tell |
|
|
|
|
|
#4 |
|
Triple-A Player
Join Date: May 2003
Posts: 150
|
schneb, there are in fact tools that allow one to customize the contextual menu to great effect. I use a variation on the New Text File script above (among many others) with a control- / right-click through BigCat, which is a contextual menu plugin that executes AppleScripts and shell scripts on Finder selections (and text selections as well, though only in Carbon apps). Also available is On My Command, with a similar feature set. And then there are function-specific plugins like FileUtilsCM, QuickImageCM, FinderIconCM, PDFViewCM, and others.
|
|
|
|
|
|
#5 |
|
Major Leaguer
Join Date: Aug 2003
Posts: 430
|
Here's another vote for AppleScript. The following script will attempt to create an empty text file named 'untitled.txt' in the front Finder window. If a file with that name already exists, the user will be prompted to enter a new name. If no Finder windows are open, the user will be given the opportunity to have the file created on the Desktop. Hook it up to a keyboard command and you're set.
Code:
set new_name to "untitled.txt"
tell application "Finder"
try
set fol_ to (target of front window)
on error
set dd to ¬
(display dialog "No Finder windows were detected. Make a text file on the desktop?" buttons ¬
{"Yes", "No"} default button "No")
if button returned of dd is "No" then
return
else
try
make new file at desktop with properties {name:new_name}
tell application "Finder" to open result
on error number -48
repeat
display dialog ¬
"A file named \"" & new_name & "\" already exists on the desktop." default answer ¬
"Enter a name that isn't in use" with icon 1
set new_name to text returned of the result
try
make new file at desktop with properties {name:new_name}
tell application "Finder" to open result
exit repeat
end try
end repeat
end try
return
end if
end try
try
make new file at fol_ with properties {name:new_name}
tell application "Finder" to open result
on error number -48
repeat
display dialog ¬
"A file named \"" & new_name & "\" already exists in the front window." default answer ¬
"Enter a name that isn't in use" with icon 1
set new_name to text returned of the result
try
make new file at fol_ with properties {name:new_name}
tell application "Finder" to open result
exit repeat
end try
end repeat
end try
end tell
Last edited by robJ; 03-05-2004 at 12:20 AM. |
|
|
|
|
|
#6 |
|
Major Leaguer
Join Date: May 2002
Location: atl, ga, usa
Posts: 344
|
Many thanks to Lankhmart and RobJ for those two AppleScripts, which helped lots of folks besides me, I'm sure. Of the two, Lank's fits my needs just perfectly. For others, I can see where RobJ's script might be just what they want.
I searched among the keystroke applications Lank listed, looking for one that would do just what I needed, that was totally free, and that seemed like it might remain so. After a few dead-ends, I tried an old copy of Youpi Key Editor that I keep around and, lo and behold, it still works, even though I've upgraded OSX twice since the last time I used this copy of Youpi Key. Youpi Key was the perfect solution, because it allows me to map almost *any* keystrokes. Now I can just type Apple-Option-Control-N in a Finder window, and Lank's excellent script pops up, asks me for a filename, which I type, pressing Return afterwards. TextEdit pops up with the file open, ready for me to fill in. This is precisely the feature I wanted to add to OSX. Again, I thank the Lank. Lank, if you're interested in such things, I think you should offer this script to the world. I think there is a site or two for sharing AppleScripts. I bet many professionals would find this feature useful -- they might not know it yet, but it's really handy. |
|
|
|
|
|
#7 |
|
Major Leaguer
Join Date: Aug 2003
Posts: 430
|
I'm glad that one of the scripts filled your need. I was on my way out when I replied so I didn' take the time to juice it up with nice features like allowing you to name the file before it was created.
-- Rob |
|
|
|
|
|
#8 | |||||||||||||||||||
|
MVP
Join Date: Dec 2003
Location: Redlands, CA
Posts: 2,300
|
Yep, I have about 50% of those, and I love them. However, I was talking native installment of OSX. Yes, Apple has this huge thing about a one button mouse. But they need to start thinking about all the power users and switchers who need the right and provide more control (such as a utility to maintain the CM) and design of this feature. But thanks for links!!
__________________
Tour Israel and Jordan via CD-ROM |
|||||||||||||||||||
|
|
|
|
|
#9 |
|
Triple-A Player
Join Date: Jun 2003
Posts: 94
|
Isn't support for 2-button mice pretty much built-in to OS X? I know it is for Panther but isn't it the same for Jaguar as well? You can just plug in most USB 2-button mice and keep right on clicking with no installation of extra software.
Now all Apple needs to do is start selling two-button mice as standard with their Macs. Oh well, maybe with the G6's....
__________________
The MacNut 20" iMac with 2 Ghz Intel Core Duo OS 10.5.8/4 GB DDR2 SDRAM List owner, AppleWorks/ClarisWorks List http://awlist.macnuthome.com Creator of the webcomic The Vanguard http://thevanguardhome.com |
|
|
|
|
|
#10 |
|
Major Leaguer
Join Date: May 2002
Location: atl, ga, usa
Posts: 344
|
This dang "MakeNewTextHere" script just recently started giving me a Quit/Run prompt before it will run -- I know someone told me that I should save the script as an Application, and make sure the Startup Screen box is unchecked when saving, but I've already done that. I've tested this over & over, making a copy of the script with a new name, and even trying saving as "Run Only" ... but every time I trigger the script with Youpi Key [which tells the Mac to "Open MakeNewTextHere.app"], I get the Quit/Run confirmation box again. I don't know what I did to cause this, but does anyone know what I can do to make it go back to the desired behavior: I hit the Youpi Key shortcut assigned to the script, and it immediately prompts for a filename?
|
|
|
|
|
|
#11 |
|
MVP
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,381
|
This is a stab in the dark, but I've noticed odd behaviour with a couple of my apps since I installed the recent security update. Property List Editor always demanded I give it permission whenever I needed to use it. My Applesscript's were fine, but I've got a feeling this maybe a random problem so maybe it applies in your case.
I can't see anything wrong in Lankhmart's script so try what I did and move the app into /Applications, which at least in my case seemed to solve the problem. |
|
|
|
|
|
#12 |
|
Major Leaguer
Join Date: May 2002
Location: atl, ga, usa
Posts: 344
|
Good thinking; I suspect something involving recent security updates, too -- in my case, though, moving it to /Applications didn't change a thing, whether I saved it as an App, or as an App with "Run Only" checked, even. I still get the Quit/Run dialog now, unless I find the script and do a CMD-O to open it, or double-click the script. If Youpi Key opens it, the script always prompts me first.
|
|
|
|
|
|
#13 |
|
Triple-A Player
Join Date: May 2003
Posts: 150
|
I would suggest pasting the script code right into a Youpi Key "Run script" shortcut instead of using an applet. It's quicker to execute and should avoid the confirmation dialog problem.
|
|
|
|
|
|
#14 |
|
MVP
Join Date: Jan 2002
Posts: 1,531
|
Is there some secret to copy/paste these new "scrolling" code windows at MacOSXHints. The script above is more than a single screen so it has a scroll bar. I cna not figure out an easy way to copy/and paste the whole script - since Safari doesn't support the select, then scroll to bottom, hold shift key and click to select all.
Whats the trick.. been bugging me for a while! |
|
|
|
|
|
#15 |
|
MVP
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,381
|
Press the "quote" button on the post as if you mean to reply. Copy from the resulting edit window. That's one way!
|
|
|
|
|
|
#16 |
|
MVP
Join Date: Jan 2002
Posts: 1,531
|
Thx bramley - I never thought of that... shouldn't have to be that way though. Why is it Apple can't just add the shift select to Safari....hrumpf!
|
|
|
|
|
|
#17 |
|
Moderator
Join Date: Jan 2002
Location: Montreal
Posts: 29,278
|
script to double height of CODE sections
I think bramley's suggestion of using the Quote button is a more practical solution, but here's a script (Perl and AppleScript) that attempts to solve the problem by redisplaying the page with all CODE sections doubled in height. It should at least be useful for educational purposes.
Code:
#!/usr/bin/perl -w
# This script is intended for situations where there is a CODE section
# on a MacOSXHints forum page and the code is long enough to require
# a vertical scrollbar. This makes it difficult to select all of the code
# since Safari doesn't auto-scroll as you drag the selection out.
# This script provides one solution to this problem - it makes Safari
# redisplay the page with all CODE sections doubled in height.
# If there is still a vertical scrollbar, run the script again
# with the temporary page as the front-most Safari window
# and it will re-double the height.
# Cameron Hayne (macdev@hayne.net), June 2004
use strict;
my $scriptname = "unscroll_vbulletin";
# runAppleScript: Runs the supplied AppleScript
# The argument is the text of the AppleScript.
sub runAppleScript($)
{
my ($ascript) = @_;
my $result = `/usr/bin/osascript<<" EOT"
$ascript
EOT
`;
chomp($result);
return $result;
}
# getSourceOfFrontWebPage: Gets the source of the frontmost web page from Safari
sub getSourceOfFrontWebPage()
{
my $ascript =<<" EOT";
tell application "Safari"
source of document 1
end tell
EOT
my $text = runAppleScript($ascript);
return $text;
}
# changeCodeHeight: Doubles the height of the CODE sections in the text
sub changeCodeHeight($)
{
my ($text) = @_;
$text =~ s/(<pre class="alt2" style="margin:0px; padding:3px; border:1px inset; width:\d+px; height:)(\d+)/$1.$2 * 2/eg;
return $text;
}
# saveTextToFile: Saves the given text to a temp file,
# and returns its name
sub saveTextToFile($)
{
my ($text) = @_;
my $tmpFile = "/tmp/$scriptname$$";
open (FILE, ">$tmpFile") || die "Can't write temp file: $!";
print FILE $text;
close(FILE);
return $tmpFile;
}
# showWebpage: Displays the given file in a Safari window
sub showWebpage($)
{
my ($filename) = @_;
my $ascript =<<" EOT";
tell application "Safari"
make new document at end of documents
set URL of document 1 to "file://$filename"
activate
end tell
EOT
my $status = runAppleScript($ascript);
return $status;
}
# -- MAIN --
my $webpageText = getSourceOfFrontWebPage();
$webpageText = changeCodeHeight($webpageText);
my $tmpFile = saveTextToFile($webpageText);
showWebpage($tmpFile);
unlink($tmpFile);
|
|
|
|
|
|
#18 | |||||||||||||||||||||||
|
Major Leaguer
Join Date: May 2002
Location: atl, ga, usa
Posts: 344
|
Thanks; I did that. The original, very convenient initial function is back: now, in any Finder window, I can do CMD-OPT-CTRL-n and immediately get a save dialog for a new TXT file, ready to be saved in the very same Finder window. I just type a title, and bang, the file is open in TextEdit, ready to type into. The only thing different now is that it exits with an error dialog box if I choose to cancel, instead of ok, in the first file name dialog box. No longer as elegant, but definitely not a problem for me. |
|||||||||||||||||||||||
|
|
|
|
|
#19 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Triple-A Player
Join Date: May 2003
Posts: 150
|
You're welcome. I'm glad it's working again.
That's easily remedied by trapping the "user cancelled" error. Here is a slightly modified version of the script that should prevent the dialog box from appearing: Code:
tell application "Finder" to set save_folder to (target of window 1) as alias tell application (path to frontmost application as Unicode text) try set text_file to choose file name with prompt "Enter file name:" default location save_folder on error number -128 --user cancelled; stop execution return end try end tell set file_ref to open for access text_file with write permission set eof file_ref to 0 close access file_ref tell application "Finder" set file type of (text_file as alias) to "TEXT" set creator type of (text_file as alias) to "ttxt" end tell tell application "TextEdit" activate open text_file end tell |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|