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



Reply
 
Thread Tools Rate Thread Display Modes
Old 09-12-2012, 07:55 AM   #1
merkuryo
Prospect
 
Join Date: Sep 2012
Posts: 4
"File ... wasn't found" error when using bash script

I'm using Mountain Lion and I'm trying to write a script to resize windows from the terminal via osascript.
Here's the script I was using:

Quote:
osascript -e "tell application \"$appname\" to set bounds of windows whose visible is true to {$bounds}"

Problem is, I get an "File <$appname> wasn't found" error.
Same line of code works fine when I type it directly on the terminal or call it as a function from my .profile file, though.

Should I be using a different format when calling osascript from bash scripts?
Thanks.
merkuryo is offline   Reply With Quote
Old 09-12-2012, 04:44 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,437
It would make it easier for us to help if you supplied the *exact* error messages that you receive.
I.e. was the error message exactly this:
"File <$appname> wasn't found"
?
If so, the problem would seem to be with 'bash' not interpolating the value of the variable 'appname'.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 09-12-2012, 05:59 PM   #3
merkuryo
Prospect
 
Join Date: Sep 2012
Posts: 4
No.. I meant that whatever string I input, it would return the error with string.
For example, if I supply "TextEdit", the error would be "File TextEdit wasn't found"
merkuryo is offline   Reply With Quote
Old 09-12-2012, 11:35 PM   #4
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,177
If that's the whole script (as you say it is), then you haven't given $appname a value. (Also, if that's the whole script, its a sh script, not a bash script. /bin/sh is the default shell for executing executable text files, and for "do shell script ..." from AppleScript. Not that it matters here, since both shells will interpret this line the same.)

Reading between the lines, I'm guessing that you have a script file with only that one line, and you're invoking the script from another shell script. (You say "Same line of code works fine when I type it...", telling me that you aren't typing it. I'm having to guess, because you haven't told us what you're doing instead.) The invoking script has a variable $appname defined, and you're expecting the invoked script to know that value.

But it won't unless the invoking script exports it. Try adding the lines

export appname
export bounds

to the invoking script, before the invocation.

Alternatively, source the file instead of invoking it. (The line works in .profile, because .profile is sourced.)
ganbustein is offline   Reply With Quote
Old 09-13-2012, 12:58 AM   #5
merkuryo
Prospect
 
Join Date: Sep 2012
Posts: 4
Sorry if my post was a bit vague but this isn't the entire script.
I have a couple of lines to check if the input for $bounds is valid.
Like I said it's a bash script so I don't type the line, I just invoke the script from the terminal and supply the application name to the variable.

Should I post the entire script? I thought this line of code would suffice since it's the only one evaluating $appname anyway.
merkuryo is offline   Reply With Quote
Old 09-13-2012, 04:17 PM   #6
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,437
Quote:
Originally Posted by merkuryo
No.. I meant that whatever string I input, it would return the error with string.
For example, if I supply "TextEdit", the error would be "File TextEdit wasn't found"

Ok so, your 'appName' variable does have the correct value.

It seems strange to me that you get a message saying that "File xxx wasn't found" instead of something about an application.
Is that the exact wording of the error message?

It would be best if you posted the whole script (or preferably just a cut-down version that illustrates the problem).
And please show us the contents of the Terminal window where you are running your script so we can see the invocation line and the resulting error messages verbatim. Just copy/paste the relevant lines here.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 09-13-2012, 05:51 PM   #7
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,177
Check that you've spelled the application name correctly. In particular, check for extra characters in the value of $appname. For example, if I enter:

Code:
appname='TextEdit '
bounds=100,200,500,700
osascript -e "tell application \"$appname\" to set bounds of windows whose visible is true to {$bounds}"
the output I get is:
Code:
36:75: execution error: File TextEdit  wasn’t found. (-43)
Notice that I supplied an extra space at the end of $appname. If you look closely, you'll notice there's a corresponding extra space in the error message. The extra character doesn't have to be a space (although a non-whitespace character would have stood out more prominently in the error message), and does not need to be at the end.
ganbustein is offline   Reply With Quote
Old 09-13-2012, 06:31 PM   #8
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,437
Quote:
Originally Posted by ganbustein
although a non-whitespace character would have stood out more prominently in the error message

Not necessarily:
Code:
% foo="TextEdit^G"
% echo $foo
TextEdit
Note that when typing the first of the above lines, I didn't type a '^' followed by a 'G' - instead I typed: Control-V Control-G to get that '^G'
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 09-13-2012, 06:37 PM   #9
merkuryo
Prospect
 
Join Date: Sep 2012
Posts: 4
Guys! Thanks for all the replies and I'm really sorry to have bothered you all but I've found the reason for my problem!
I saved the script on my desktop so it's easier to edit it and have been running the script from the desktop. But when I copied it to the /usr/local/bin folder and ran it from there it worked!
merkuryo is offline   Reply With Quote
Reply

Tags
bash, mountain lion, osascript, terminal

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 11:07 AM.


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.