PDA

View Full Version : Trouble with applescript




greenythebeast
Jan 4, 2012, 08:58 AM
Let me say firstly that I'm an applescript newbie :)
Can someone please tell me why this AS code:
display dialog "Please enter justin username:" with title "Enter Username" default answer ""
set cbUsername to text returned of result as text
do shell script "rtmpdump -r 'rtmpe://edge1-a.stream.justin.tv/live-edge' -a 'live-edge' -W 'http://ccstatic.justin.tv/static/flash/CBV_2p49.swf' -p 'http://justin.tv/" & cbUsername & "/ -y 'mp4:public-" & cbUsername & "' -v -o ~/t.flv -T 'm9z#$dO0qe34Rxe@sMYxx%' --verbose"
is producing:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file" number 2
as an error?



subsonix
Jan 4, 2012, 09:38 AM
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file" number 2
as an error?

I think your error is related to the nesting of " and ' in you shell pipeline. It's a pretty large IMO, and it's also doing the majority of the work here, the Applescript is merely throwing up a dialog. I would suggest that you save the shell script in a file instead and avoid most of the quotes.

jared_kipe
Jan 4, 2012, 09:58 AM
Let me say firstly that I'm an applescript newbie :)
Can someone please tell me why this AS code:
display dialog "Please enter justin username:" with title "Enter Username" default answer ""
set cbUsername to text returned of result as text
do shell script "rtmpdump -r 'rtmpe://edge1-a.stream.justin.tv/live-edge' -a 'live-edge' -W 'http://ccstatic.justin.tv/static/flash/CBV_2p49.swf' -p 'http://justin.tv/" & cbUsername & "/ -y 'mp4:public-" & cbUsername & "' -v -o ~/t.flv -T 'm9z#$dO0qe34Rxe@sMYxx%' --verbose"
is producing:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file" number 2
as an error?

This would appear to be an unmatched ' (the fix is in red, but hard to see. it's on the third line after cbUsername)

display dialog "Please enter justin username:" with title "Enter Username" default answer ""
set cbUsername to text returned of result as text
do shell script "rtmpdump -r 'rtmpe://edge1-a.stream.justin.tv/live-edge' -a 'live-edge' -W 'http://ccstatic.justin.tv/static/flash/CBV_2p49.swf' -p 'http://justin.tv/" & cbUsername & "/' -y 'mp4:public-" & cbUsername & "' -v -o ~/t.flv -T 'm9z#$dO0qe34Rxe@sMYxx%' --verbose"

Does the -o token (~/t.flv) need a set of 's as well?

greenythebeast
Jan 4, 2012, 10:06 AM
This would appear to be an unmatched ' (the fix is in red, but hard to see. it's on the third line after cbUsername)

display dialog "Please enter justin username:" with title "Enter Username" default answer ""
set cbUsername to text returned of result as text
do shell script "rtmpdump -r 'rtmpe://edge1-a.stream.justin.tv/live-edge' -a 'live-edge' -W 'http://ccstatic.justin.tv/static/flash/CBV_2p49.swf' -p 'http://justin.tv/" & cbUsername & "/' -y 'mp4:public-" & cbUsername & "' -v -o ~/t.flv -T 'm9z#$dO0qe34Rxe@sMYxx%' --verbose"

Does the -o token (~/t.flv) need a set of 's as well?

Nice catch! And no, the -o doesn't need to be quoted. Thanks for the help!

greenythebeast
Jan 7, 2012, 03:57 AM
Found another puzzling piece of code that isn't working for me...
tell application "Finder"
if exists file "~/" & cbFilename & ".flv" then
set x to 1
display dialog "Filename already exists" with title "Error" with icon stop
repeat
set newCbFilename to cbFilename & "_" & x as string
if exists file "~/" & newCbFilename & ".flv" then
set x to x + 1
else
exit repeat
end if
end repeat
copy newCbFilename to cbFilename
end if
end tell

I'm trying to have the AS see if a filename already exists and if it does, change the name. What am I doing wrong? Remember, I'm an AS newbie so please don't be harsh on me! :confused:

Mr. Retrofire
Jan 7, 2012, 05:46 AM
Found another puzzling piece of code that isn't working for me...

I'm not sure which problem you try to solve, so i assume you have problems with the POSIX path/alias conversion? The code below demonstrates the trick.


on AS_POSIX_FileExists(fileName, pathSuffix)

-- we work with POSIX paths, so let us construct one
set myHomePath to POSIX path of (path to home folder)
set myFileName to fileName & pathSuffix
set myFilePath to myHomePath & myFileName

try
-- only existing files/folders can have alias references
-- aliases are standard file/folder references within AppleScript
set myFileAlias to (myFilePath as POSIX file) as alias
display dialog myFileName & " exists in this folder:" & return & myHomePath
return true
on error
-- non-existing files do not have alias references
display dialog "Does not exist, that damn thing!"
return false
end try

end AS_POSIX_FileExists

on run
set theFlag to AS_POSIX_FileExists("something", ".flv")
get result -- display result within the AppleScript editor
end run

greenythebeast
Jan 7, 2012, 07:26 AM
I'm not sure which problem you try to solve, so i assume you have problems with the POSIX path/alias conversion? The code below demonstrates the trick.


on AS_POSIX_FileExists(fileName, pathSuffix)

-- we work with POSIX paths, so let us construct one
set myHomePath to POSIX path of (path to home folder)
set myFileName to fileName & pathSuffix
set myFilePath to myHomePath & myFileName

try
-- only existing files/folders can have alias references
-- aliases are standard file/folder references within AppleScript
set myFileAlias to (myFilePath as POSIX file) as alias
display dialog myFileName & " exists in this folder:" & return & myHomePath
return true
on error
-- non-existing files do not have alias references
display dialog "Does not exist, that damn thing!"
return false
end try

end AS_POSIX_FileExists

on run
set theFlag to AS_POSIX_FileExists("something", ".flv")
get result -- display result within the AppleScript editor
end run

With the code I pasted, even when a file does exist it just continues the script and writes over it instead of renaming it like I want it to

greenythebeast
Jan 7, 2012, 07:51 AM
Alright I figured it out using this code:
tell application "Finder"
if exists "/Users/Greeny/" & cbUsername & ".flv" as POSIX file then
set x to 1
repeat
set newCbFilename to cbUsername & "_" & x as string
if exists file newCbFilename then
set x to x + 1
else
exit repeat
end if
end repeat
copy newCbFilename to cbUsername
display dialog "Filename already exists
" & "File will be named: " & newCbFilename & ".flv" buttons "OK" default button "OK" with title "Error" with icon stop
end if
end tell

Thanks for your help :) What would be the easiest way to make that home path universal as opposed to hard coded to my own home folder path?

Mr. Retrofire
Jan 7, 2012, 08:20 AM
What would be the easiest way to make that home path universal as opposed to hard coded to my own home folder path?

Apples standard additions provide the following function
path to home folder

This returns a standard reference (alias). You can get the POSIX path of the home folder via:

POSIX path of (path to home folder)

Apples standard additions define many other standard folders, which you can use in your apple scripts.

greenythebeast
Jan 7, 2012, 08:48 AM
One more question. Is there a way for applescript to tell when Terminal finishes a shell command and is idle?

Mr. Retrofire
Jan 7, 2012, 07:02 PM
One more question. Is there a way for applescript to tell when Terminal finishes a shell command and is idle?

Probably not. That would require some form of IPC (Inter Process Communication) between the current process in the Terminal and your AppleScript process. Most shell tools which run within the Terminal do not support Apple Events, which means you have no way to know, what a process within the Terminal does. You can use "do shell script" function for your projects.

subsonix
Jan 7, 2012, 08:02 PM
"run shell script" causes the calling process to wait until the shell script has finished. The same way system() works in C.

greenythebeast
Jan 8, 2012, 09:20 AM
I'm using "do script" because I want a Terminal window to show up. Does that work the same way as "run shell script"?

subsonix
Jan 8, 2012, 02:28 PM
"do shell script", I somehow missed that you were invoking it from Terminal. Apple has a tech note on "do shell script" that you might find useful.

greenythebeast
Jan 10, 2012, 10:38 AM
Yay, more questions! Is it possible to set the title of a Terminal window using Applescript?

chown33
Jan 11, 2012, 04:13 PM
Yay, more questions! Is it possible to set the title of a Terminal window using Applescript?

You should be able to answer this yourself. It's no different in principle than any other programming problem.

First, consult the reference on what Terminal is capable of doing with windows. If the reference says you can set a title, then you have the answer. If it doesn't say, then you next consult a general reference on what most programs are capable of doing with windows.

So where does one find the reference on what Terminal is capable of doing with windows using AppleScript? In Terminal's scripting dictionary. How does one get access to Terminal's scripting dictionary? In AppleScript Editor, File menu, Open Dictionary, then choose Terminal.app.

All the information about scriptable objects and their properties is in the scripting dictionary. Simply break the problem down and find the relevant info. "Break it down" means browsing the dictionary terms, finding the window object (or document, or whatever represents an open shell window), and seeing if it has a title property that's writable.

Once you find something that looks promising, try it and see what happens. If it works as expected, problem solved. If not, try variations. If nothing works, ask here, but be prepared to describe what you tried, what you expected to happen, and what actually happened.


The process of breaking problems down, referring to documentation, and trying things is not unique to AppleScript. It's at the heart of all programming, and when you think about it, it's the fundamental way that all problems are solved: break it down, consult references, try something.

greenythebeast
Jan 11, 2012, 04:35 PM
Thanks for the stern suggestion, I probably needed it. I did as you told me and looked up Terminal's dictionary and have the following code:

tell window 1
set title displays shell path to false
set title displays window size to false
set title displays file name to false
set title displays device name to false
set title displays custom title to true
set custom title to cbUsername
end tell

Works great if I'm opening up Terminal from scratch. However, if there are already terminal windows open, it renames the wrong window. Is there a "current window" parameter for tell?

greenythebeast
Jan 13, 2012, 05:50 PM
Could really use some help here :( I'm genuinely stumped and I've looked everywhere that I know for an answer with no luck.

chown33
Jan 13, 2012, 06:24 PM
Look up how you refer to windows in general (should apply to every scriptable app). In particular, look for front window, active window, etc.

If you don't have a reference on AppleScript, you should probably get one. It could be a book, it could be a website with reference docs on it. In any case, the basic principles of AppleScript should be the same. If you don't know them, you don't have a solid foundation to build on.

If you haven't done a tutorial on AppleScript, you probably should. Example page from one:
http://www.macosxautomation.com/training/applescript/12.html
Found by googling: applescript front window

It's also unclear to me whether you're using AppleScript Editor to send simple messages to apps. Like telling Terminal to get all windows. Then you should be able to see what windows it has, or how they're numbered. You need to see how things work simply and interactively, before you can script them effectively. Example:
tell app "Terminal"
get properties of front window
end tell
This applies to every scriptable app, not just Terminal. Try it with Finder, TextEdit, Safari, etc.

Finally, it's unclear to me whether your tell window 1 block is the entire script (which won't work), or exactly how you've bracketed it with a tell app "Terminal" block. You need to post code that shows the complete context. Isolated fragments of code are useless. Example:
tell app "Terminal"
tell front window
set title displays custom title to true
set custom title to "Xyzzy"
end tell
end tell
The above works for me on 10.6. It doesn't create a new window if a window already exists.