Applescript to quit Terminal problem

Discussion in 'Mac Programming' started by gugge99, Feb 8, 2011.

  1. gugge99 macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #1
    First of all, I am completly new to Applescript!

    My problem is that I use Applescript to run some commands in Terminal and want to quit Terminal when all the commandlines are finished executing. Currently I'm using only a delay of 5 sec, but this is not always enough. The execution of the command lines varies from 1 sec up to perhaps a minute.
    I've been running a while loop to check if a specified process is active and then run a delay, but it is not enough.
    So what I want is actually:

    IF (Terminal is done executing) QUIT
    ELSE (Wait untill done)

    I've been searching the forums and seen that it might be possible to not run Terminal with Applescript (meaning do something like: tell application "Terminal".....) to run command lines. Anyone know anything about that?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
  3. gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #3
    Thanks for the fast response! Here is the code where I want to change something.

    Code:
    	tell application "Terminal"
    		activate
    		do script "cd dropbox/master/latexproj" in front window
    		do script "pdflatex -shell-escape latexproj.tex" in front window
    		delay 2
    		tell application "System Events"
    			repeat while (name of every process) contains "pdftex"
    				delay 5
    			end repeat
    		end tell
    		delay 2
    		quit
    	end tell
    
     
  4. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    I'm confused, according to chown33's link
    Wouldn't that avoid the need for a pause?

    I'd probably add the "-interaction batchmode" to pdflatex so it doesn't get confused on any errors.

    B
     
  5. LPZ macrumors 65816

    Joined:
    Jul 11, 2006
    #5
    Why are you using Applescript at all?

    You want to switch to a certain directory and create a pdf from a .tex file using pdflatex.

    If you tell us your overall goal we can likely provide a better solution.
     
  6. gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #6
    thanks again for all the replies!
    tried to change the "do script" to "do shell script" but then I had trouble with accessing the directory of which I am going to do the command in.

    Ok, lets tell you a little bit more about my project.
    I am writing in Latex and using XCode as my editor. In addition to my latex I use TikZ to make images and graphs. As my pictures/graphs sometimes is big (approx 3Mb in text format) the compilation of my latex code is taking FOREVER. To solve this problem I compile each picture/graph to pdf, so that I don't have to compile every picture/graph when I want to build my latex project.
    The problem with doing it this way, is that if I have compiled my pic/graph, the changes I make later will not be visible untill I compile the pic again. Which means that I would have to delete the old pdf and compile again.

    And the solution that I'm looking for here is actually. Press Build Button in XCode and everything compiles correctly.
    This means:
    1: Check if the tikz file is newer than the corresponding pdf file
    2: If there is some newer tikz files, delete the corresponding pdf file and run the compiling commands in Terminal and quit when the commands are done.
    3: If there is no tikz file that is newer than the pdf files compile regular in XCode.

    I hope this explains a little bit more for you. If not, don't hessitate to ask.
    By the way, I'm writing the Applescript in Automator and don't know if that is an issue. (Starting up a Applescript project in XCode was way to complicated for me :D)
    Thanks
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    When you change your code, post the new code. The 'cd' only applies to the same shell script (technically, same shell process), so if you made a line-for-line change, you've made a mistake. Of course, I'm only guessing at what you did, and I hate debugging by guessing.
     
  8. gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #8
    Sorry about that, The old "working"
    Code:
    do script "cd dropbox/master/latexproj" in front window
    do script "pdflatex -shell-escape latexproj.tex" in front window
    
    The new, not working
    Code:
    do shell script "cd dropbox/master/latexproj" in front window
    do shell script "pdflatex -shell-escape latexproj.tex" in front window
    
     
  9. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    The link chown33 posted has several examples of how to change working directory before executing a command.

    Once upon a time, I had LaTeX set up with make so I could have it do what was needed to build a long document. Most of the figures were gnuplot and it was smart enough to re-render them if I made a change to the source.

    B
     
  10. LPZ macrumors 65816

    Joined:
    Jul 11, 2006
    #10
    This seems odd. Why not use something like TeXShop?
     
  11. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    Read the provided link.

    You need to combine those as one "do shell script" separated with semicolons.

    B
     
  12. gugge99, Feb 8, 2011
    Last edited: Feb 9, 2011

    gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #12
    Tried
    Code:
    do shell script "cd ~/dropbox/master/latexproj;pdflatex -shell-escape latexproj.tex"
    
    but it wouldn't compile

    Code:
    do shell script "cd ~/dropbox/master/latexproj;ls"
    
    worked, but I didn't see any answers. EDIT: I manage to see it now.
     
  13. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #13
    :confused: Copy and paste from your post compiles fine here.

    B
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    Exactly what happened when it worked?

    What do you mean by "didn't see any answers"?

    1. Describe what you expected to happen.
    2. Describe what actually happened.
    3. Post actual output, not your description of it.
    4. If there are errors, post actual error message texts.
    5. Post complete code.


    So what you're really looking for is for the pdflatex commands to run as part of a Build process in Xcode.

    Xcode has a Run Script Build Phase that can be added to any target. You can even add multiples of such build phases. I see no reason to go through the convoluted process of running an AppleScript simply to execute shell commands.

    I recommend that you do the following...

    1. Make a copy of your Xcode project.
    2. Remove everything but a few source files.
    3. Make sure it still builds.
    4. Remove the build directory.
    5. Archive the resulting project and upload it somewhere like dropbox.com.
    6. Post the public URL here.

    Why I'm asking for this...

    We can only guess at your project structure. We have no idea how it's currently setup to build. Yet adding a shell script build phase requires knowledge of the project and its structure. So in order to advise what to put in the build phase, we need to see the actual project.

    We don't need to see the whole project, just enough so it builds and demonstrates the pdflatex or other handling you want to accomplish.

    I'm not guaranteeing that anyone will look at your project and tell you exactly how to add a shell script build phase. However, I can pretty well guarantee that the path you're following now, with AppleScript and whatever else you haven't posted, will not work reliably, if at all. I base this on the fact that unless you understand how Xcode's dependency analysis works, it will be impossible to make something that relies on Xcode's dependency analysis. And in case it's unclear, your 3-step summary of what you want to happen is exactly what a dependency analysis is: figuring out what actions to take based on file dates. And the actions to take: those are defined in the Run Script Build Phase.
     
  15. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #15
    dejo posted this link in the iOS Programming forum FAQ.

    http://www.mikeash.com/getting_answers.html

    Read the article and ask yourself if you are asking questions in a way that will get you the answers you seek.

    FWIW I just tried this (back on the Mac, was on iPad earlier) and found that I had to provide a full path to pdflatex (/usr/texbin/pdflatex in my case) but otherwise do shell script worked perfectly well for a TeX file I had lying around.

    Code:
    do shell script ("cd [I][B]<TEXPATH>[/B][/I];pwd;[I][B]/usr/texbin/[/B][/I]pdflatex [I][B]TestBook[/B][/I]")
    substitute the bits in bold italics per your needs.

    B
     
  16. gugge99, Feb 9, 2011
    Last edited: Feb 9, 2011

    gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #16
    When it worked, I mean it compiled. And when I said "didn't see any answers" I meant that I can't be sure that it worked, as I couldn't see "ls" happening. As I would if I go into the terminal and wrote "ls".
    As you might understand, I don't really know the difference between a shell command/line and writing something in Terminal. The way I get around the problem today is by going to Terminal writing two command lines and then everything is ok. I'm just trying to avoid it and press Build in XCode and everything will work.

    I've just made a dummyProject, and it can be downloaded from: http://dl.dropbox.com/u/13082737/dummyProject.zip

    I've put the figure files in the folder figure. The tikz files are in figure/tikz. Have also put some extra code lines in the tikzfiles to change the figure. Hope this helps
     
  17. gugge99, Feb 9, 2011
    Last edited: Feb 9, 2011

    gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #17
    Tried this
    Code:
    do shell script "cd ~/dropbox/master/latexproj; /usr/texbin/pdflatex -shell-escape latexproj.tex"
    
    it works if all the pdf figures are up-to-date. If not it gives me the message: "sh: pdflatex: command not found". I think this is really strange cause it also tells me that there is syntax errors if I try again.
     
  18. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #18
    I presume the output is going to a console log windows, just like when you compile anything in Xcode. Do you use Xcode for code or just LaTeX?

    FWIW I was testing in Script Editor and the output is clearly visible there.

    B
     
  19. gugge99, Feb 9, 2011
    Last edited: Feb 9, 2011

    gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #19
    For the Applescript I have been using Automator by adding a Applescript to my workflow. I thought that this was going to be simple... obviously not :)

    Tried this, and it is obviously better than Automator! Thank you, now I can see the output!
     
  20. gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #20
    My whole code is now:
    Code:
    do shell script "cd ~/dropbox/master/latexproj;pwd;/usr/texbin/pdflatex -shell-escape latexproj.tex"
    
    If all my figures are present as pdf-files this works as usual (Just like the Build button in XCode or just as pdflatex latexproj.tex). The problem arise when I have deleted the pdf-files, then the result from the script is:
    Code:
    tell current application
    	do shell script "cd ~/dropbox/master/latexproj;pwd;/usr/texbin/pdflatex -shell-escape latexproj.tex"
    		--> error "sh: pdflatex: command not found" number 1
    
     
  21. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #21
    That's where make is a better tool than an AppleScript for this kind of stuff.

    It should be able to tell if your figures are out of date, and create them first before trying pdflatex.

    B
     
  22. gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #22
    I am not following. Make?
     
  23. gugge99 thread starter macrumors newbie

    Joined:
    Feb 8, 2011
    Location:
    Trondheim, Norway
    #24
    Ok, thanks for the suggestion but I don't think this is something for me. Already used a lot of hours getting the Applescript to nearly function and would rather use my bug-program than have to use another 10-20 hours to set this up and learn it. In the end, I am only trying to work this out to save time. Sofar I've used to much time working on my Applescript.
     
  24. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #25
    Which is why LPZ and others have questioned your choice of tools. Xcode and Applescript vs. TexShop or even emacs/Aquamacs.

    You are trying to adapt a Phillips screwdriver to turn a flathead screw. Sure, you can probably get it to work with enough effort, but it will never be as ideal as using the right tool in the first place.

    B
     

Share This Page