Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 07-16-2011, 03:35 AM   #1
ph-apoc
Prospect
 
Join Date: Jul 2011
Posts: 9
Question Modify variable that contains list of files

Hello there,

basically what I want to do is in my eyes not very complicated but I am not coming around one "simple" problem.

Basically what I am doing

> get files from finder action
> get path of files action
> get unix path of files
> save this into a variable.

the output until this point is:
"/test/test/test1.jpg""/test/test/test2.jpg""/test/test/test3.jpg"

So far so good. Everything what is between me and my goal: missing spaces between those paths.
The result should look like this:
"/test/test/test1.jpg" "/test/test/test2.jpg" "/test/test/test3.jpg"

I need the spaces because otherwise the shellscript I am triggering afterwards will not recognise the paths correctly.

Anybody has a hint for me how to modify the variable to get the output I need? Everthing I tried and found via google did not work :/

Thanks in advance
ph-apoc is offline   Reply With Quote
Old 07-16-2011, 06:39 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,459
Perhaps the discussion of list separators here:
http://daringfireball.net/2003/02/sa...re_safari_urls
will be of use.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 07-17-2011, 06:56 AM   #3
ph-apoc
Prospect
 
Join Date: Jul 2011
Posts: 9
Hello Hayne,
you definitly are my hero of the day for that link! I spend hour after hour and really was frustrated. Now had a look, read the page and this is what I got:

Quote:
on run {list_of_pics}
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set list_of_pics to list_of_pics as text
set AppleScript's text item delimiters to old_delim

tell application "Terminal"
activate
tell window 1
do script "jhead -n%Y-%m-%d_%H-%M-%S " & list_of_pics
end tell
end tell
end run

And it worked like it should Wonderful. Many thanks!!!
ph-apoc is offline   Reply With Quote
Old 07-17-2011, 10:18 AM   #4
ph-apoc
Prospect
 
Join Date: Jul 2011
Posts: 9
Well, I was to happy as it seems.

It works when I am using the "step-by-step" functionality within automator but when I am using the workflow in finder the string

"/test/test/test1.jpg" "/test/test/test2.jpg" "/test/test/test3.jpg"

gets to

/test/test/test1.jpg /test/test/test2.jpg /test/test/test3.jpg

which is bad if there are spaces within the path :/
Any idea why this is happening ?
ph-apoc is offline   Reply With Quote
Old 07-17-2011, 10:49 AM   #5
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,459
You'll need to escape the quotes so they get preserved until needed, or (more reliable) break up the string into separate paths and then create the final string for your shell script as needed.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 07-17-2011, 05:00 PM   #6
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,191
If you tread down the path of escaping quote marks, you'll quickly find yourself in a morass. What do you do with paths that contain quote marks? You need to escape those, too. But if you escape them with backslashes, what do you do with paths that contain backslashes? It might be easier to use single quotes, but then you still have to deal with paths that contain single quotes.

Fortunately, you don't have to deal with any of that. "quoted form of ..." is your friend. It'll take care of all the messy quoting rules.

Automator and AppleScript usually prefer to deal with (lists of) aliases or HFS-style paths, in which colon is the path separator and slash is a normal character; Unix uses POSIX-style paths, where slash as the path separator and colon as a normal character. The fix is to exchange those two characters before passing a path to the shell. But there's also a difference in how they distinguish relative from absolute paths. In HFS-style, an absolute path is a path that contains colons but doesn't begin with one (and the disk name must be specified as the first path component, even if it's the boot path); POSIX-style identifies an absolute path as one beginning with a slash (and the disk name is omitted, so a path to a non-boot volume must explicitly wend its way through to the volume's mount point, usually in /Volumes).

Fortunately, you don't need to deal with any of that either. "... as text" converts an alias or file to an HFS-style path; "POSIX path of ..." converts an HFS-style path to the equivalent POSIX-style path, and "quoted form of ..." makes Terminal happy. In the other direction, "POSIX file ..." converts a POSIX-style path to a file; "alias ..." converts a file or an HFS-style path to an alias, and everybody except Terminal is happy working with aliases

Assuming list_of_pics is already a list of POSIX paths:
Code:
on run {list_of_pics}
	set command to "echo jhead -n%Y-%m-%d_%H-%M-%S"
	repeat with aPic in list_of_pics
		set command to command & " " & quoted form of aPic
	end repeat
	tell application "Terminal"
		activate
		do script command
	end tell
end run
Notice I took out the "tell window 1" block as irrelevant (and error-prone, because there might not be a window 1). Terminal always opens a new window for a "do script ..." message.

In the more usual case where list_of_pics is a list of aliases:
Code:
on run {list_of_pics}
	set command to "echo jhead -n%Y-%m-%d_%H-%M-%S"
	repeat with aPic in list_of_pics
		set command to command & " " & quoted form of POSIX path of (aPic as text)
	end repeat
	tell application "Terminal"
		activate
		do script command
	end tell
end run
ganbustein is offline   Reply With Quote
Old 07-20-2011, 04:14 AM   #7
ph-apoc
Prospect
 
Join Date: Jul 2011
Posts: 9
Hello ganbustein,

thanks very much for your reply and your explanations. I tried to implement this scriptlet and it works, well, a bit.

The situation is a bit strange... within the automator-pane everything works fine, but when I run it as a service under finder

the variable list_of_pics however gets from
'/test/test/test1.jpg' '/test/test/test2.jpg' '/test/test/test3.jpg'

to
'/test/test/test1.jpg' '/test/test/test2.jpg' '/test/test/test3.jpg' '/test/test/test1.jpg' '/test/test/test2.jpg' '/test/test/test3.jpg'

Any idea why this is happening?

I played around with the script-sniplet from you but did not get any other "logic" than: in automator it works (step-by-step)
within the context of finder it is not working..
ph-apoc is offline   Reply With Quote
Old 07-21-2011, 08:20 PM   #8
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,191
Check the input to the "Run AppleScript" step, by adding a "View Results" step just before it. I'll bet you'll find that the list of files has already been duplicated before it even reaches your script.

A Service takes some externally-specified data and operates on it. For example, you might have specified: "Service receives selected (files or folders) in (Finder.app)" at the top of the service workflow.

But for testing purposes, you probably included a "Get Selected Finder Items" step (because when you're testing, it isn't a Service yet, and doesn't have any input).

The result is that your Service now gets the Finder selection (because it's a Service) and also gets the Finder selection (because you told it to). If you get the Finder selection twice, you process it twice.
ganbustein is offline   Reply With Quote
Old 07-22-2011, 04:38 PM   #9
ph-apoc
Prospect
 
Join Date: Jul 2011
Posts: 9
Well, ganbustein I think you know it already, but you are totally right...

I am really feeling like a complete idiot... somehow I try to find excuses but, well - forget it. It is quiet logical what you are telling me but somehow I did not get it by myself.

Is there any good book out there which explains such basics of automator and apple-scripting you would recommend for a newbie?

Thanks a lot. Finally something of my "todo-list" which I can mark as "done".
ph-apoc is offline   Reply With Quote
Old 07-23-2011, 04:48 PM   #10
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,191
Not just a book, but a whole school. The School of Hard Knocks. How do you think we know this stuff? We've all already made the same mistakes ourselves. We say "Don't step there, step here," and sound so smart, because we usually omit the rest of the sentence: "because 'there' is where I stepped and fell flat on my ... ."

For AppleScript, I can recommend "AppleScript: The Definitive Guide" by Matt Neuburg. I've never read a book on Automator, so can't help you there.
ganbustein is offline   Reply With Quote
Reply

Tags
automator, modify, spaces, text, variable

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:46 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.