New to forum. A couple of questions if I may?

IamRob. Some people who request help in this forum just want a solution that works–and that works quickly–and are not overly concerned if they understand it or not. For years I used scripts that Shane and Nigel wrote, which I did not understand, but they were indispensable to me.

In the past, Shane has emphasized that threads such as yours should be considered as a database of sorts which others will consult in the future. This makes absolute sense to me, and, for this reason, the question of whether you prefer one script over another is of little importance IMO.

Glad I could help :slight_smile: I remember when I started writing AppleScripts there was a lot of incomprehension and frustration on my part. Gradually I got to learn how to use the bits I needed: everyone has to start somewhere.

AppleScript is definitely used for automating applications, but it has its own bunch of tricks too.

One of them is ASObjC (AppleScript Objective C), which accesses macOS’s underlying code frameworks. As @peavine’s script shows, it is often much faster than ‘vanilla’ AppleScript, but it isn’t essential to do much of what you need.

It basically uses a second language within AppleScript. AS was designed from the start to be “English-like”. It has its quirks, but it’s relatively straightforward to pick up and follow. ASObjC is another matter entirely. IMO.

Up to macOS Mojave it was possible to use third-party scripting additions to extend the functionality of AppleScript. There were text-processing, list processing, XML processing, REGEX additions that a many scripters (myself included) found incredibly useful. These all stopped working thanks to new security procedures in Mojave. A lot of their functionality is available within ASObjC - at the expense of learning a second, much more complex, language.

Is there a way to carry over the file name that was clicked on in “longText”'s choose file prompt, over to “shortFile”'s choose prompt and add a suffix to it?

The file name “Untitled” shows up and forces me to retype the whole name.

In Script Editor, pull down the File menu and choose Open Dictionary…

Choose Standard Additions.

In the Dictionary window, use search box to find the entry for Choose File Name. It has an optional parameter: [default name].

Here’s one way of using this:

set longTextFile to (choose file with prompt "Choose a file to chop down:")
tell application "System Events" to set longTextFileName to name of longTextFile
set AppleScript's text item delimiters to "."
set textItems to text items of longTextFileName
set {originalName, originalExtension} to {item 1 of textItems, item 2 of textItems}
set shortTextFileName to originalName & "-shortened." & originalExtension
set shortTextFileAlias to (choose file name default name shortTextFileName with prompt " Create a new file for the shortened text:")
set longText to read longTextFile
set parCount to count paragraphs in longText
set shortText to ""
repeat with x from 1 to parCount
	set nextPar to paragraph x of longText
	set charCount to count characters in nextPar
	if charCount > 48 then
		set shortText to shortText & text 1 thru 48 of nextPar & return
	else
		set shortText to shortText & nextPar & return
	end if
end repeat
set shortFileWrite to (open for access file shortTextFileAlias with write permission)
try
	write shortText to shortFileWrite
on error
	close access shortFileWrite
end try
close access shortFileWrite
display dialog "Finished chopping!" buttons {"OK"} default button 1 giving up after 30

Another option would be for the script to create the name and save the shortened file in the same location without any user interaction. But this raises the question of what you would want to happen if a file with the same name as the shortened file already existed? Or if you wanted to place the new file in a different location?

You might also want to handle the case of you (or your user) choosing an incorrect file type (for instance a .jpg). And how would you process multiple files without going through repeated “choose file” dialogs? It’s all possible, but it comes with added complexity.

Awesome.

Now that’s the king of automation I like.

I thought about asking for a fully automatic ability, without user interaction, to process every file in a given directory, but I thought I might be overstepping my welcome for asking for so much. What you have given me so far has been an incredible learning tool as well as a useful tool.

Thanks for that.

You will run into an issue with this part of the code if the file you choose has more than one “.” (period) in it’s name. Only the last period would designate the extension.

set longTextFile to (choose file with prompt "Choose a file to chop down:")
tell application "System Events" to set longTextFileName to name of longTextFile
set text item delimiters to "." -- no loner need to use "Applescript's" before text item delimiters
set textItems to text items of longTextFileName
set {originalName, originalExtension} to {item 1 of textItems, item 2 of textItems}

you could change the last line above to:

set {originalName, originalExtension} to {(items 1 thru -2 of textItems as text), item -1 of textItems}

Another issue would be if the file has no extension at all.

You would have to test for both outcomes and parse accordingly.

Additionally the last part where the file gets written, a try statement should be around the “open for access” line, as that is where the errors usually occur

try
	set shortFileWrite to (open for access file shortTextFileAlias with write permission)
on error
	display alert "Error creating shortened file!" giving up after 30
	return
end try
try
	write shortText to shortFileWrite
end try
close access shortFileWrite
display alert "Finished chopping!" giving up after 30

I spent some time learning more about character encoding and have modified my ASObjC script in post 18 to correct this issue. It should be noted that the lines of this script that fix the character-encoding issue were provided by Nigel. The timing result with my 10,000-line text file was reduced from 2 to 1.4 seconds.