Trouble with applescript

Discussion in 'Mac Programming' started by greenythebeast, Jan 4, 2012.

  1. macrumors regular

    Joined:
    Mar 24, 2008
    #1
    Let me say firstly that I'm an applescript newbie :)
    Can someone please tell me why this AS code:
    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:
    Code:
    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?
     
  2. macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    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.
     
  3. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #3
    This would appear to be an unmatched ' (the fix is in red, but hard to see. it's on the third line after cbUsername)

    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 & "/[COLOR="Red"]'[/COLOR] -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?
     
  4. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #4
    Nice catch! And no, the -o doesn't need to be quoted. Thanks for the help!
     
  5. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #5
    Found another puzzling piece of code that isn't working for me...
    Code:
    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:
     
  6. macrumors 601

    Mr. Retrofire

    Joined:
    Mar 2, 2010
    Location:
    www.emiliana.cl
    #6
    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.

    Code:
    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
     
  7. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #7
    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
     
  8. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #8
    Alright I figured it out using this code:
    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?
     
  9. macrumors 601

    Mr. Retrofire

    Joined:
    Mar 2, 2010
    Location:
    www.emiliana.cl
    #9
    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.
     
  10. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #10
    One more question. Is there a way for applescript to tell when Terminal finishes a shell command and is idle?
     
  11. macrumors 601

    Mr. Retrofire

    Joined:
    Mar 2, 2010
    Location:
    www.emiliana.cl
    #11
    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.
     
  12. macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    "run shell script" causes the calling process to wait until the shell script has finished. The same way system() works in C.
     
  13. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #13
    I'm using "do script" because I want a Terminal window to show up. Does that work the same way as "run shell script"?
     
  14. macrumors 68040

    Joined:
    Feb 2, 2008
    #14
    "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.
     
  15. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #15
    Yay, more questions! Is it possible to set the title of a Terminal window using Applescript?
     
  16. macrumors 603

    Joined:
    Aug 9, 2009
    #16
    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.
     
  17. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #17
    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:

    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?
     
  18. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #18
    Could really use some help here :( I'm genuinely stumped and I've looked everywhere that I know for an answer with no luck.
     
  19. chown33, Jan 13, 2012
    Last edited: Jan 13, 2012

    macrumors 603

    Joined:
    Aug 9, 2009
    #19
    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:
    Code:
    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:
    Code:
    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.
     

Share This Page