TextEdit long delay for .sh

Hi,

Has any one else experienced TextEdit taking a very long time to save a .sh file?

Edited: in Mavericks?

Thanks,
kel

Hello.

Nope.

I actually like the idea of using TextEdit to make a quick and dirty shell script.

You are at your own peril of course if you try to make a file containing rich text executable by the script below. :slight_smile:

tell application id "ttxt"
	set docPath to path of its front document
	try
		set probe to docPath
	on error
		display alert "Save first..."
		error number -128
	end try
end tell
set shFile to POSIX path of docPath
set sfxOfs to offset of ".txt" in shFile
try
	if sfxOfs is not 0 then
		set newSFile to text 1 thru (sfxOfs - 1) of shFile
		
		do shell script "mv " & quoted form of shFile & space & quoted form of newSFile & "; chmod u+x " & quoted form of newSFile & "; SetFile -c \"ttxt\" -t \"ttxt\" " & quoted form of newSFile
		set docPath to POSIX file newSFile as alias as text
	else
		do shell script "chmod u+x " & quoted form of shFile & "; SetFile -c \"ttxt\" -t \"ttxt\" " & quoted form of shFile
	end if
on error e number n
	display dialog e
end try


tell application id "sevs" to tell disk item docPath
	set default application of it to "com.apple.textedit"
end tell


If you want to execute it from TextEdit, then you’d use something like this.

use AppleScript version "2.3"
use scripting additions
use Output : script "Output"

tell application id "ttxt"
	set docPath to path of its front document
	try
		set probe to docPath
	on error
		display alert "Save AND make executable first..."
		error number -128
	end try
end tell
set shFile to POSIX path of docPath
try
	set theResult to do shell script quoted form of shFile
on error e number n
	display dialog e
end try

if theResult is not "" then Output's initwithtext("Output From: " & shFile & linefeed, 600, 800, theResult)
Output's leaveAtFront(0.5)

Hi McUsr,

I don’t know what happened, but before, it was taking several seconds to save. Now it is less than a second. It fixed itself.

Thanks for the scripts,
kel

Well. I thought it was a good idea, lets have it as simple as possible, when we know what we do.

it isn’t more dangerous than to do it in the Terminal actually. :slight_smile:

I elaborated a little. I had a slight quarrel with launchd, and realized that I can’t trump its definitions of uti’x for file extensions. Then there is TextEdit, that always adds a txt extension, to some unknown extension.

I bypass this, by giving shell scripts I inted to execute from the Desktop the extension “.shellscript” or whatever else, except for any “known” extension like “.sh”. Then the file is made as an executable, it gets a black nice icon, and you’ll look intot the file information window and see that it opens in textEdit.

Then you can double click.

I’d be much obliged if you tested it kel, so that I know that what I have written makes any sense. :slight_smile:

Thanks.

Edit

You need to have Developer tools, command line tools installed, so that you have the SetFile command around.

Hi McUsr,

Yes, it is working great. It opens in TextEdit, but is executable with your second script. TextEdit is like a script editor! I think I’ll place your scripts in the Scripts Menu for TextEdit.

Edited: btw, when I save to file with an extension other than .txt, TextEdit doesn’t add the .txt extension.

Thanks a lot,
kel

Hello.

Thank you. :slight_smile: I take it that you have TextEdit set up in preferences to not add a txt extension, if an extension is missing. Then the script won’t do anything. The problem is that you really can’t use an .sh extension, and rely on it to open in TextEdit again, as it won’t. That is the caveat. I live good with using .shellscript, to signify that this is someting I’ll open in TextEdit, and that way, no accidents won’t happen. But maybe adding the extension, and maybe a different icon with RCDefaultPreferences pane, will make the visual difference bigger, but an undetected file extension is shown, so it is no biggie. :slight_smile:

Watching the World Championship in Chess nowadays, I think we have the most geeky sports news in the world, with analysis of chess matches. :slight_smile:

Hi McUsr,

That’s what I did, use the shell script extension. The .sh would extension would run the script in Terminal since I have .sh to open with Terminal.

I used to be a chess fanatic reading the books and magazines around the time of Spassky .vs Fischer matches. I never could beat those internet robots though and many of the humans also. :slight_smile:

Thanks again for the scripts,
kel

Hello.

Well, you kind of instigated the shell scripts, by giving me a chance to thinking it through, a quick retrospective analysis . :smiley: revealed that I sometimes, could benefit from having quick access to shell scripts.

It is no replacement for TextWrangler or anything, but the truth is that TextEdit is open most of the time anyway, and when I have scripts that are more like “jobs” that are to be run, then the it can be fast to just open such scripts into TextEdit, and run them from there! :slight_smile:

I read all the matches between Karpov and Kasparov, (after Fischer), I also like the Chess.App that are in Mac Os X, -and Grapher when I am at it. I also think this new operating system did something with my productivity. It is great: you just fan it all out on different desktops, so you can have man full screen windows, it is really great with a flattened UI, and it is so easy and intuitive to use. And I really love the two monitor thing, because then you have continuity when you switch screen, as you have half of your working area intact. I love it! And everything concerning spaces/desktops doesn’t really need to be scripted or anything, The gestures, control keys, and function keys makes it all in all a pleasure to use. (And one hot corner for Mission Control.) “Houston” :smiley:

Hello.

By the way, when you are generating output with TextEdit, then I guess you want the script below: It asks you if it should close all unsaved documents, and does so if you accept. I find it very useful, but then again, I have a lot of stuff the displays its output in an unsaved TextEdit document. :slight_smile:

script closeUnsavedDocs
	script o
		property L : missing value
	end script
	
	tell application id "ttxt"
		set o's L to path of its documents
		local dc
		set dc to length of o's L
		if dc > 0 then display dialog "Really close all unsaved documents ?" with title "Close every unsaved TextEdit document" with icon 1
		repeat with i from dc to 1 by -1
			if item i of o's L is missing value then
				close document i saving no
			end if
		end repeat
	end tell
end script
tell closeUnsavedDocs to run

It’s great knowing AppleScript huh. You can mold the program to your needs. I changed the bounds of the result window. It needs a lot more work with more checking and perhaps changing the names of the document.

tell application id "ttxt"
	activate
	set docPath to path of its front document
	try
		set probe to docPath
	on error
		display alert "Save AND make executable first..."
		error number -128
	end try
end tell
set shFile to POSIX path of docPath
try
	set theResult to do shell script quoted form of shFile
on error e number n
	display dialog e
end try
if theResult is not "" then
	tell application id "ttxt"
		set {x1, y1, x2, y2} to (bounds of front window)
		set rbounds to {x1, y2 + 1, x2, y2 + 1 + 300}
		set new_doc to (make new document with properties {name:"Result"})
		set bounds of window "Result" to rbounds
		set text of new_doc to "OUTPUT FROM : " & shFile & return & return & theResult
	end tell
end if

Hi McUsr,

Your first two scripts are very useful. Instead of using Terminal to Change the permission, I could have been using a script all this time. Running the script that runs the shell script was another great simple idea! Instead of double clicking it every time. I don’t know about the saving the results part, because the users can just save it themselves in TextEdit. The priority for me was more just seeing the results. You’re at the higher stage where you’re actually using the results.

I still need to look more into your other ideas, but they look good to me at a quick glance.

The new Apple chess app is great. I can actually beat it now when it makes mistakes. :smiley:

Have a good day.

Later,
kel

Hi McUsr,

I’ve found a bug when trying to close the “Result” window. Need to look into this further.

Edited: also, it looks like the way we created the windows are the same. I need to look into that.

Later,
kel

I found the stupid bug I think. I was creating windows with the same name. So I need to name the result windows according to the script windows. That’s what I was planning in the first place. I hope that’s the reason.

Hi McUsr,

Do you place all your libraries in “Script Libraries” now or just the ASObjC in there?

Later,
kel

Found this with ‘defaults read com.apple.finder’:
IconViewSettings = {
arrangeBy = none;
backgroundColorBlue = 1;
backgroundColorGreen = 1;
backgroundColorRed = 1;
backgroundType = 0;
gridOffsetX = 0;
gridOffsetY = 0;
gridSpacing = 54;
iconSize = 64;
labelOnBottom = 1;
showIconPreview = 1;
showItemInfo = 0;
textSize = 12;
viewOptionsVersion = 1;

I wonder if that’s the defaults.

The main thing I wanted to know was what the default icon size is. It must be 64.

Thanks a lot,
kel

Hello.

I actually have some aspirations of putting at least some Libraries in the /Library/Script Libraries folder, that is libraries with terminology, and that is over and dealt with.

Nice to see defaults for finder, when it comes to icons, I used to change the size, to cram as many as possible into the window when I used the 12 inch. -I don’t care about the size nowadays, (as long as they aren’t bigger than 64).

I have updated the library above with methods for for initWithName, and initWithTextAndName. I realize I have to do something with the printit handler now, and will come back with something after I have given it some thought.

Have a nice evening.

Oops. I posted my last post in the wrong thread. It was meant for View Options.

Hi McUsr,

Yes, giving document names is probably not a good idea. I have to think of a better way. I’ll look at the rest later when I get up. You’ve been busy.

Thanks a lot,
kel

Hi McUsr,

I couldn’t sleep, so decided to do some things around the house. Keeping busy. :slight_smile:

I think text files can have any extension. When it is not specified by the user, then it should be .txt.

Was thinking that the result window could be saved to the temporary items folder. Then when the user closes the result window, TextEdit won’t get mixed up. It then doesn’t matter if the user would revert changes or save. On running the script, the new result replaces the old text if there was any.

Edited: btw, if TextEdit won’t allow anything other than .txt, then System Events can always change the extension.

Edited: wow! You really did add a lot of changes to the library.

Later,
kel

Hi McUsr,

I see what you’re doing now after some sleep.

My TextEdit preferences is set to open with rtf. Many times I want a new plain text document, so I would open a new document and change to plain text.

Sometimes I have some tab delimited text and want to create a table in an rtf document.

Sometimes I want to create a list with a formatted script and add line numbering like to a copied script from Applescript Editor.

TextEdit is not very scriptable. So, you might make some ui scripting handlers for editing the text of the front document.

I’ll write back if I think of more. I was thinking of doing some things in conjunction with Notes.app also.

I’ve been walking around in a daze all day, but got a lot done. :slight_smile:

Have a good day,
kel

If you’re a bit familiar with Cocoa and making applications scriptable (quite easy in Cocoa), TextEdit from Apple is open source software and you can modify it to make it more scriptable if you like. My copywriters work with a modified version of text edit as well.