Checking for text at the end of a string and adding if missing

Hi,
I’ve written an applescript that re-sizes postscript files based on a percentage.

I’m basically just passing a bunch of variables to sed to find and replace values in the .ps file.

I would like to have a save as dialogue for the re-sized file that is created.

I’ve created a variable to get a user created file name. See below:

set outputFile to choose file name with prompt "Set file name and location of the reduced file:"

What I would like it to be able to check the string for a .ps file extension and add it if it’s missing.

I’m very new to Applescript and scripting in general and feeling a bit lost.

Any help would be greatly appreciated.

Thanks,
Chris

First of all welcome!

String concatenation is quite easy in AppleScript because you only need the ampersand between two strings (or variable holding the string). So you example would look like this.

There is also a begins with and ends with operator that works for lists and strings.

set outputFile to choose file name with prompt "Set file name and location of the reduced file:"
if outputFile does not ends with ".ps" then set outputFile to outputFile &  ".ps"
--now outputFile always ends with .ps

Hi DJ. Don’t you have to convert outputFile to text first?


set outputFile to choose file name with prompt "Set file name and location of the reduced file:"
set thePath to outputFile as text
if thePath does not end with ".ps" then
	set thePath to thePath & ".ps"
end if

return thePath

Otherwise you would get {file “Mac OS X:Users:DJ:test”, “.ps”}

Correct! I haven’t tested the code but choose file names doesn’t return a string so you need to coerce it first.


set outputFile to (choose file name with prompt "Set file name and location of the reduced file:") as string
if outputFile does not end with ".ps" then set outputFile to outputFile & ".txt"
--now outputFile always ends with .ps

Thanks guys. That was super easy.

My script is working pretty damned well if I do say so myself.

So, I have another question.

The first part of the script os a dialogue asking for a file to resize, the next is what was discussed above. Choosing a filename and location for the resulting file.

What if I want to pass the path from the user selected file on to the default path in the save as dialogue?

This is what I have at the moment:

# Choose Postscript file to resize
set inputFile to (choose file with prompt "Select a Postscript file to resize:" of type {"ps"})

# Get proper UNIX path to Postscript file
set p_inputFile to POSIX path of inputFile

# Choose file name for reduced postscript file and force a .ps file extension
set outputFile to (choose file name with prompt "Set file name and location of the reduced file:" default name "reduced_file.ps")
set outputFile to outputFile as text
if outputFile does not end with ".ps" then
	set outputFile to outputFile & ".ps"
end if

# Get proper UNIX path to  reduced Postscript file
set p_outputFile to POSIX path of outputFile


Right now p_inputFile is returning something like:
/Users/UserName/Desktop/filename.ps

How would I strip the filename.ps portion of the variable and use it as the default path for the outputFile dialog?

Thanks,
Chris

Hi,

try this


set inputFile to (choose file with prompt "Select a Postscript file to resize:" of type {"ps"})
tell application "System Events" to set fileName to name of inputFile

# Get proper UNIX path to Postscript file
set p_inputFile to POSIX path of inputFile

# Choose file name for reduced postscript file and force a .ps file extension
set outputFile to (choose file name with prompt "Set file name and location of the reduced file:" default name "reduced_" & fileName)
set outputFile to outputFile as text
if outputFile does not end with ".ps" then
	set outputFile to outputFile & ".ps"
end if

# Get proper UNIX path to reduced Postscript file
set p_outputFile to POSIX path of outputFile