Apparently simple problem is becoming extremely difficult

First, I wanted to say, “Hello, world.” I came across these forums while working out some applescripts that I am trying to write to supplement a Filemaker database that I have been building. I should state up front that I am a novice coder, with experience limited mostly to Python and a primary school education in BASIC/Pascal. I have perused the tutorials and the Mac Developer Library and I have been unable to work through this problem on my own.

I am trying to write a program that takes a specific text file (specified by the user), calls a Perl script that converts that text into .SVG vector graphics, and then uses an ingenious (in my opinion) quicklook hack that generates a .PNG that I would like to embed in a Filemaker container.

Here’s the code that I’ve written (in short):

set thefile to POSIX path of (choose file) do shell script "perl /Users/JoelR/Documents/Scripts/ape2svg.pl -i " & thefile as text set newfile to (thefile as text) & ".svg" set thefolder to folder of thefile do shell script "qlmanage -t -s 640 -o " & thefolder & " " & newfile as text
The first three lines execute just fine. I have no problems getting the perl script to generate the .SVG and “newfile” looks like the POSIX path of the .svg that the script generates. I am unable to “strip” the folder pointer from the “thefile” alias. Because of this, I am unsure if the qlmanage call will even work.

The quicklook hack (which I found here) does not work as is. The current directory (specified by a period) symbol does not seem to work in this context. Anyways, I’m trying to tell quicklook to:

generate a thumbnail of my .SVG (option -t)
resize that thumbnail to 640x640 (option -s 640)
output to directory (option -o & thefolder)

all on the file that the user selected (newfile as text)

I understand that this is convoluted and that I could probably get a similar result with ImageMagick, but I want this script to run natively on Lion without any added packages. Does anyone know how to rectify this problem?

Thanks in advance!
Joel

Model: Macbook Air
AppleScript: 2.43
Browser: Safari 537.4
Operating System: Mac OS X (10.7)

Hello Joel and welcome to MacScripter! :slight_smile:

The problem is that AppleScript in itself isn’t that Posix path savy, nore folder structure save when it comes to splitting things into parts.

I just provide you with this little System Events snippet to extract the posix path of the folder, and advise you to use System Events for such operations, as System Events are much smarter with regards to Posix paths than Finder is, which is basically your only other option, before resorting to file and pathname manipulating routines, which may be a better approach. (I’ll provide such a handler as well.)


tell application "System Events"
    set thefolder to POSIX path of (container of disk item thefile)
end tell

This one returns the posix path to the folder of a posix file, without trailing “/”


to pxFolder for aPxFile
    local tmp, ofs, tids
    set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
    set tmp to reverse of (characters of aPxFile) as text
    set ofs to (offset of "/" in tmp) + 1
    set tmp to (reverse of (characters ofs thru -1 of tmp) as text)
    set AppleScript's text item delimiters to tids
    return tmp
end pxFolder

The current folder of Apple Script is “/”, so you have to cd to it first, before using ./somefolder.

Use something like below at the start of your do shell script! :slight_smile:

 "cd " & quoted form of fullPxPathtoYourWorkfolder & "; " & "  the rest of the line that uses ./"

(The semicolon in a separate “clause” was just illustourous.) It has to be there in order for you to be able to execute several commands in a single do shell script.

Good luck, and I hope you share your results! :slight_smile:

Hi Joel.

A shell script way to get the folder of a file (given its POSIX path) is with “dirname”. It’s not as fast as either of the methods McUsr’s suggested, but it may fit into your script stylistically.

set thefolder to (do shell script ("dirname " & thefile))

Since ‘thefile’ and ‘newfile’ are already text values, there’s no need for the 'as text’s in your code. However, it’s usually a good idea to use the ‘quoted form’ of paths in shell scripts, to avoid possible confusion with spaces and other characters in the shell script texts.

set theFile to POSIX path of (choose file)
do shell script "perl /Users/JoelR/Documents/Scripts/ape2svg.pl -i " & quoted form of theFile
set newfile to theFile & ".svg"
set thefolder to (do shell script ("dirname " & quoted form of theFile))
do shell script ("qlmanage -t -s 640 -o " & quoted form of thefolder & " " & quoted form of newfile)

By the way, this site has [applescript] and [/applescript] tags for code which format it as above with a clickable link to open the code in people’s default script editors.

More accurately: “. the posix path to the folder of a file indicated by a posix path .” A ‘POSIX file’ is a particular thing in AppleScript. :slight_smile:

A less convoluted way to do the same thing would be:


to pxFolder for aPxFile
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	set tmp to text 1 thru text item -2 of aPxFile
	set AppleScript's text item delimiters to tids
	return tmp
end pxFolder

Both, however, are specialised for files and assume there’s no trailing slash in the input path.

I wasn’t quite awake at the time I guess :stuck_out_tongue:

Hello

As most of the time I’m trying to play safe, I would use this edited handler :


to pxFolder for aPxFile
	if aPxFile ends with "/" then set aPxFile to text 1 thru -2 of aPxFile
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	set tmp to text 1 thru text item -2 of aPxFile
	set AppleScript's text item delimiters to tids
	return tmp & "/"
end pxFolder

to hfsFolder for aHfsFile
	if aHfsFile ends with ":" then set aHfsFile to text 1 thru -2 of aHfsFile
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set tmp to text 1 thru text item -2 of aHfsFile
	set AppleScript's text item delimiters to tids
	return tmp & ":"
end hfsFolder

#=====

set p2l to path to library folder from user domain

tell application "System Events"
	path of container of p2l
	(*Macintosh HD:Users:currentAccount:*)
end tell
POSIX path of result
(*/Users/currentAccount/*)

pxFolder for POSIX path of p2l
(*/Users/currentAccount/*)

hfsFolder for p2l as text
(*Macintosh HD:Users:currentAccount:*)

To be consistent with what we get using path of container, I added a slash (or a colon) at the end of the returned string.

Yvan KOENIG (VALLAURIS, France) samedi 20 octobre 2012 16:24:21

Hello Yvan.

You are indeed perfectly right, your version works as good as mine, there is no need for removing the trailing slash but for aestethic reasons, so your handler is indeed the correct one, for consitstency, which is important.

But, System Events for instance doesn’t follow this convention. There are quirks here, and some adjustments will have to be made one way or the other.

But if my handler was a library handler, and not one I wrote to solve a one off problem for a fellow scripter, then I’d follow your reasoning, and agree fully hearted to it. :slight_smile:

(I am actually to consolidate my handlers to handle both, or start to only use those that handles both name styles, to minimize the number of handlers. (Like System Events. :))

By the way Yvan;“Sed oneliners” is intersting to google. :wink:

Wow! Very impressive everyone. Thanks again.