Do shell script problem in Applescript
Hi,
I am an Applescript novice and have been trying to write a code to go to a particular folder, look for all files in the folder tree with extension .m2v and run an executable file to decode them. My problem is that when I run my code (containing do shell script), it searches through all files and folders on Mac HD and starts decoding .m2v files elsewhere that I don't want.
Eventually it runs out of space (.m2v file decoding takes a lot of space), because it is dumping all decoded .yuv files onto the HD.
When I run the command on Terminal, it executes the decoding perfectly and stores the decoded files in the same folder.
Please help me about what's going on.
My code is something like:
tell application "Finder"
set DestinationFolder to "xxxxxx:xxxx:xxxx"
set NumFolders to (get count of folders under Destination folder)
repeat for SomeVar from 1 to NumFolders
set FolderinQuestion to folder SomeVar of DestinationFolder
-- Tried tell application "Terminal" here, but did not know --how to export the FolderinQuestion variable from Finder to --Terminal
do shell script " \" cd \" & (POSIX path of (result as text));
for file in `find $pwd \"*.mov\"`
do
/usr/local/bin/decode file
done"
end repeat
end tell
I would greatly appreciate some guidance.
The root of the problem is that you're trying to quote the cd command for some reason:
<pre class=command>do shell script " \" cd \" & (POSIX path of (result as text));
...</pre>
In addition to that you're including the & (POSIX path of (result as text)) as part of the shell command whereas this should be OUTSIDE of the quotes in order to get evaluated
If you work that through you'll end up with a shell command that looks like:
<pre class=command>" cd " & (POSIX path of (result as text))</pre>
If you try to run that in a terminal you'll get a cd : command not found error and that's why the rest of it appears to fail.
The solution to that one is simple - just don't bother quoting the cd and put the POSIX path stuff outside of the quotes to get it evaluated at runtime:
<pre class=command>do shell script "cd " & quoted form of POSIX path of (FolderInQuestion as text)) & ";
# rest of shell commands here"</pre>
Now, as for the rest of the script there are a few things I would change.
First, unless you need to know the index, don't do:
>repeat for SomeVar from 1 to NumFolders
set FolderinQuestion to folder SomeVar of DestinationFolder
the issue is that the number of folders to process may change during the script's execution (other processes may create or remove folders). This will, at best, cause some folders to be skipped and, at worst, cause the script to fail.
If you're iterating through a list, the best option is to just:
<pre class=command>repeat with FolderInQuestion in (folders of DestinationFolder)
...</pre>
This automatically sets the iterator (in this case, FolderInQuestion, to the first item in the list and increments it for each iteration through the loop.
Secondly, in your shell script itself, scrub the entire do/done loop. You're already using find, so have that do the hard work using the -exec switch:
<pre class=command>find path -name "*.mov" -exec /usr/local/bin/decode {} \;</pre>
In find's case, {} is substituted with the current file's path.
Putting this together you'd get:
<pre class=command>tell application "Finder"
set DestinationFolder to "xxxxxx:xxxx:xxxx"
repeat with folderInQuestion in (get folders of folder DestinationFolder)
do shell script "cd " & quoted form of POSIX path of folderInQuestion & "; find . -name \"*.mov\" -exec /usr/bin/decode {} \\;"
end repeat
end tell</pre>
Note that I've used 'quoted form of POSIX path' - this takes care of any shell-unsafe characters like spaces in the path name. I've also used \\; for the -exec switch - this is so that AppleScript passes the \ to the shell command rather than using it for its own escaping.
But you're not done yet!
There's still one fatal flaw in this process - and that is the fact that find by default, is recursive - it will walk through every directory that it finds.
This means that if you start at the top folder and iterate through, find will find all .mov files and decode them. Your script then cd's to the first subdirectory and repeats the process - decoding all the .mov files in that directory and all its subdirectories even though they've ALREADY been decoded.
The upshot is that you only need to run one loop starting at the top level. You don't need to iterate through all the subdirectories since find will do that for you.
In addition to that, there might not be a need to use cd at all since the first argument to find is the directory to start searching in. Unless there's some reason that you need to start decode from the top level directory (e.g. is that where it saves the files?), you can drop the whole repeat loop altogether and just run with:
<pre class=command>set startFolder to (choose folder)
do shell script "find " & quoted form of posix path of startFolder & " -name \"*.mov\" -exec /usr/bin/decode {} \\;"</pre>
That's the entire script - a radical compression of your original.
Similar Messages
-
Please help me remove the last elements of shell script from my applescript
I read somewhere that running a shell script in an applescript was inefficient and created extra overhead, fine, so I am trying to take all of my shell script out of my applescript (this should fix issues with forward slashes in names as well...)
however I am stuck on these last two lines (I know they are probably trivial but I've been up waaaay too long now) so I was wondering if anyone would be awesome enough to show me how to convert the bash to applescript.
--does a recursive copy and rename
do shell script "cp -R " & (thePath as text) & " " & (theOtherPathWithFile as text)
--does a copy and rename, then a simple move
do shell script "cp " & (quoted form of (POSIX path of this_item as text)) & " " & (theNewPath as text) & "&& mv " & (theOtherPathWithFile as text) & " ~/Desktop"
Any and all help will be appreciated.There isn't anything wrong with using shell scripts, especially since AppleScript is designed to do this very thing. There is some overhead when using do shell script, just as there is some overhead when using an application tell statement, so it just depends on what you are doing.
The performance of any particular script would also be up to whatever is acceptable to you (or your customers), and sometimes a shell script is the most efficient way to do something. Many of the system utilities are just GUI front-ends to a shell script, so they can't be all bad.
To do your file copy or move with the Finder, you will need to tell it to duplicate or move the item(s) (there is that pesky overhead again). Shell scripts use POSIX paths while the Finder uses colons as its path delimiter, so you will need to use the correct file path references - there is a decent article about that at Satimage. Once your file paths have been defined (or using something like choose file or choose folder)), your script would be something like:
<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
tell application "Finder"
duplicate source to destination
-- move source to destination
end tell
</pre>
See the Finder and *System Events* scripting dictionaries for more information about the ways they deal with files. -
Can I embed a full shell script inside an applescript?
I have a friend who is running Tiger on a PPC Mac and wants to download a large number of files from the web. Unfortunately, this friend is barely able to do basic web browsing with Safari or Firefox.
I thought of just sending him a shell script with a lot of curl commands, but I don't suppose making it executable on my Mac would make it executable on his. I would like to be able to send him an Applescript that he could just run by clicking on it, but it would be awkward to make each curl command a separete shell script within the applescript..
Is there a way of directly including in an applescript a multi-line shell script as a single entity that invokes only one shell? I know I can do it by putting the shell script in a separate file and have the applescript give it the necessary permissions, but then I'd have to explain to my friend where to put the shell script!While it is possible to do this in the Applescript if the shell script gets at all complicated escaping characters and debugging will be much harder then it needs to be.
For example taking twtwtw's example and just adding one Applescript variable gives:
set dir to POSIX path of (choose folder)
set ss to "cd " & dir & "
echo 'This is a file list for the \"" & dir & "\" folder'
echo
ls -l"
set dlf to do shell script ss
display alert dlf giving up after 10
Twtwtw's suggestion of creating an Applescript application bundle is, I believe, the best way to go. You can keep the shell script and Applescript separate making maintenance and debugging much simpler and your friend just gets one 'file' to install and run.
regards -
Executing native library under Java - shell script problem
I am running a Java application on the command line bash shell. I have a few JAR files in the directory and a few native libraries. When I run the application using the command line, all works fine. This is the command I use:
java -classpath MyGame.jar:log4j-1.2.16.jar:jme/jme-colladabinding.jar:jme-audio.jar:jme-awt.jar:jme-collada.jar:jme-editors.jar:jme-effects.jar:jme-font.jar:jme-gamestates.jar:jme-model.jar:jme-ogrexml.jar:jme-scene.jar:jme-swt.jar:jme-terrain.jar:jme.jar:jogl/gluegen-rt.jar:jogl/jogl.jar:jorbis/jorbis-0.0.17.jar:junit/junit-4.1.jar:lwjgl/jinput.jar:lwjgl/lwjgl.jar:lwjgl/lwjgl_util.jar:lwjgl/lwjgl_util_applet.jar:swt/windows/swt.jar:jbullet/jbullet-jme.jar:jbullet/asm-all-3.1.jar:jbullet/jbullet.jar:jbullet/stack-alloc.jar:jbullet/vecmath.jar:trove-2.1.0.jar:sceneMonitor/jmejtree_jme2.jar:sceneMonitor/propertytable.jar:sceneMonitor/scenemonitor_jme2.jar:sceneMonitor/sm_properties_jme2.jar -Djava.library.path="lwjgl/native/linux" -Xmx1024m -Xms768m -ea com.mygame.MainThis works fine and the application starts up as expected. LWJGL native library is loaded in and works fine as expected.
The problem occurs when I try to run this command via the shell using a shell script. Here is my script:
#!/bin/bash
# Set the minimum and maximum heap sizes
MINIMUM_HEAP_SIZE=768m
MAXIMUM_HEAP_SIZE=1024m
if [ "$MYAPP_JAVA_HOME" = "" ] ; then
MYAPP_JAVA_HOME=$JAVA_HOME
fi
_JAVA_EXEC="java"
if [ "$MYAPP_JAVA_HOME" != "" ] ; then
_TMP="$MYAPP_JAVA_HOME/bin/java"
if [ -f "$_TMP" ] ; then
if [ -x "$_TMP" ] ; then
_JAVA_EXEC="$_TMP"
else
echo "Warning: $_TMP is not executable"
fi
else
echo "Warning: $_TMP does not exist"
fi
fi
if ! which "$_JAVA_EXEC" >/dev/null ; then
echo "Error: No Java environment found"
exit 1
fi
_MYAPP_CLASSPATH="MyGame.jar:log4j-1.2.16.jar:jme/jme-colladabinding.jar:jme-audio.jar:jme-awt.jar:jme-collada.jar:jme-editors.jar:jme-effects.jar:jme-font.jar:jme-gamestates.jar:jme-model.jar:jme-ogrexml.jar:jme-scene.jar:jme-swt.jar:jme-terrain.jar:jme.jar:jogl/gluegen-rt.jar:jogl/jogl.jar:jorbis/jorbis-0.0.17.jar:junit/junit-4.1.jar:lwjgl/jinput.jar:lwjgl/lwjgl.jar:lwjgl/lwjgl_util.jar:lwjgl/lwjgl_util_applet.jar:swt/windows/swt.jar:jbullet/jbullet-jme.jar:jbullet/asm-all-3.1.jar:jbullet/jbullet.jar:jbullet/stack-alloc.jar:jbullet/vecmath.jar:trove-2.1.0.jar:sceneMonitor/jmejtree_jme2.jar:sceneMonitor/propertytable.jar:sceneMonitor/scenemonitor_jme2.jar:sceneMonitor/sm_properties_jme2.jar"
_VM_PROPERTIES="-Djava.library.path=\'lwjgl/native/linux\'"
_MYAPP_MAIN_CLASS="com.mygame.Main"
$_JAVA_EXEC -classpath $_MYAPP_CLASSPATH $_VM_PROPERTIES -Xmx${MAXIMUM_HEAP_SIZE} -Xms${MINIMUM_HEAP_SIZE} -ea $_MYAPP_MAIN_CLASSThe shell script is in the same directory as the JAR files (the same directory where I ran the Java command above). When I execute the shell script ( sh MyGame.sh ), I get the UnsatisfiedLinkError message:
14-Feb-2011 19:46:28 com.wcg.game.DefaultUncaughtExceptionHandler uncaughtException
SEVERE: Main game loop broken by uncaught exception
java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:82)
at org.lwjgl.Sys.<clinit>(Sys.java:99)
at org.lwjgl.opengl.Display.<clinit>(Display.java:130)
at com.jme.system.lwjgl.LWJGLDisplaySystem.setTitle(LWJGLDisplaySystem.java:118)
at com.wcg.game.WcgStandardGame.initSystem(WcgStandardGame.java:287)
at com.wcg.game.WcgStandardGame.run(WcgStandardGame.java:185)
at java.lang.Thread.run(Thread.java:662)I don't understand what I am doing wrong. I am executing the exact same command via a shell script and it is not working. Any ideas, solutions, most welcome.
I am running Linux Mint Debian 201012, Linux mint 2.6.32-5-amd64 #1 SMP Thu Nov 25 18:02:11 UTC 2010 x86_64 GNU/Linux. JDK is 1.6.0_22 64-bit. I have 64-bit .so files in the correct place too.
Thanks
RizThanks for the replies guys/gals.
I have modified the script and echoed my command that should be running under the shell script, it is:
java -classpath WcgFramework.jar:WcgPocSwordplay.jar:log4j-1.2.16.jar:jme/jme-colladabinding.jar:jme-audio.jar:jme-awt.jar:jme-collada.jar:jme-editors.jar:jme-effects.jar:jme-font.jar:jme-gamestates.jar:jme-model.jar:jme-ogrexml.jar:jme-scene.jar:jme-swt.jar:jme-terrain.jar:jme.jar:jogl/gluegen-rt.jar:jogl/jogl.jar:jorbis/jorbis-0.0.17.jar:junit/junit-4.1.jar:lwjgl/jinput.jar:lwjgl/lwjgl.jar:lwjgl/lwjgl_util.jar:lwjgl/lwjgl_util_applet.jar:swt/windows/swt.jar:jbullet/jbullet-jme.jar:jbullet/asm-all-3.1.jar:jbullet/jbullet.jar:jbullet/stack-alloc.jar:jbullet/vecmath.jar:trove-2.1.0.jar:sceneMonitor/jmejtree_jme2.jar:sceneMonitor/propertytable.jar:sceneMonitor/scenemonitor_jme2.jar:sceneMonitor/sm_properties_jme2.jar -Djava.library.path="lwjgl/native/linux" -Xmx1024m -Xms768m -ea com.mygame.MainI am more confident that now the shell script should be fine (I am a shell script noob) because this very command if I copy from terminal and paste into the terminal, runs the application no problem at all. But I am amazed that it is still not working. I must be doing something obviously wrong. :-(
I used the code as suggested:
_VM_PROPERTIES='-Djava.library.path="lwjgl/native/linux"'I am stumped!? :-(
Thanks for help. -
Ananomous block inside shell script problem
I am having problem using / inside the anonymous block...
I have a shell script which is somethinglike this.
$ORACLE_HOME/bin/sqlplus -s << EOF
--set head off;
--set feedback off;
set serverout on;
Cursor c1 is
select --------
Begin
for x in c1 loop
Begin
<do something>
End;
End Loop;
End;
End;
EOF
mail -s -----If i don't use / certian things are not executed.
If i use exit instead of /, then it will not reach the end ie., the mail part.
can somebody guide me ??sb92075 wrote:
sunil_dba wrote:
I am having problem using / inside the anonymous block...anonymous blocks begin with
DECLARE
which posted code seems to be lackingOr also a BEGIN if no variables, constants or user types are declared. -
Sqlldr ops$user/password in a shell script - problem
Hello All:
i run the sqlldr command from a unix shell script that has
sqlldr ops$user/password@alias control='/path1/control.ctl' log='/path2/log.log'
I am geeting invalid user/password because of the $ sign in the username. What is the workaround for this? How to handle this in the script?
I tried single quote, USERID= unsuccessfully. Any ideas?
Thanks
San~Hello,
Single quote should work fine , can you post your shell script specially couple of line and try connecting from command line.
$ sqlplus 'test$user'/*****
SQL*Plus: Release 10.2.0.1.0 - Production on Tue Apr 14 14:36:04 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine optionsRegards
Edited by: OrionNet on Apr 14, 2009 2:38 PM -
Simple shell script problem...
I am trying to get a list of directories on a remote machine, compare them with the directories on the local machine and copy any that dont match on the local machine to a backup folder. Heres my code but i am a newbie and i cant get my shell script to read from a file. The for loop never executes so i am guessing i am not reading the file correctly - see below.
#!/bin/ksh
ssh -l removeserver 192.168.xxx.xxx ls /remote/server/directory/ > /store/remote/directory/listing/motapp1files.txt
newfile=''
$newfile < /store/remote/directory/listing/motapp1files.txt
for file in $newfile;
do echo $file
if [ ! -d local/machine/directories/$file/ ]; then
echo "this folder doesnt exist on on remote server "; echo $file;
fi
doneRe-inventing the wheel perhaps. Look into rsync. Great program!
I used it in FreeBSD to do a very similar task. -
Hello,
While running the following rman commands directly from the shell , its work fine :
$ rman CATALOG=catman/man@rman TARGET=/
RMAN> run {
2> allocate channel 'dev_1' type 'sbt_tape'
3> parms 'ENV=(OB2BARTYPE=Oracle8,OB2APPNAME=bz,OB2BARLIST=bz_archive,OB2BARHOSTNAME=bz)';
4> allocate channel 'dev_2' type 'sbt_tape'
5> parms 'ENV=(OB2BARTYPE=Oracle8,OB2APPNAME=bz,OB2BARLIST=bz_archive,OB2BARHOSTNAME=bz)';
6> restore archivelog from time "to_date('2011-02-14 08:31:55','YYYY-MM-DD HH24:MI:SS')" until time 'SYSDATE';
7> }
allocated channel: dev_1
channel dev_1: sid=323 devtype=SBT_TAPE
channel dev_1: Data Protector A.06.11/243
allocated channel: dev_2
channel dev_2: sid=438 devtype=SBT_TAPE
channel dev_2: Data Protector A.06.11/243
Starting restore at 2011-02-14 19:01:38
....When trying to run it as shell script :
#!/bin/ksh
ssh -l oracle 172.22.xx.xx . /software/oracle/.profile ;
export NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
rman CATALOG=catman/man@rman TARGET=/
run {
allocate channel 'dev_1' type 'sbt_tape'
parms 'ENV=(OB2BARTYPE=Oracle8,OB2APPNAME=bz,OB2BARLIST=bz_archive,OB2BARHOSTNAME=bz)';
allocate channel 'dev_2' type 'sbt_tape'
parms 'ENV=(OB2BARTYPE=Oracle8,OB2APPNAME=bz,OB2BARLIST=bz_archive,OB2BARHOSTNAME=bz)';
restore archivelog from time "to_date('2011-02-14 08:31:55','YYYY-MM-DD HH24:MI:SS')" until time 'SYSDATE';
}The script succeded to connect to rman but it stuck hir:
$ ./yoav.sh
Recovery Manager: Release 10.2.0.4.0 - Production on Mon Feb 14 20:13:41 2011
Copyright (c) 1982, 2007, Oracle. All rights reserved.
connected to target database: BZ (DBID=3044220964)
connected to recovery catalog database
RMAN> Any suggestion why it stuck ?
ThanksIt is stucked because RMAN it waiting input on STDIN and you don't give anything as input to STDIN.
Try to use
$ rman CATALOG=catman/man@rman TARGET=/ <<EOF
<your RMAN script>
EOFor try to use RMAN command file like a SQL file:
http://download.oracle.com/docs/cd/B19306_01/backup.102/b14192/setup001.htm#sthref206
Edited by: P. Forstmann on 14 févr. 2011 20:05 -
Hi all,
I know this is a little bit off subject but i have a shell scripting problem and was wondering if any of you could help...
so:
L=`wc -l $CATALINA_HOME/webapps/EmailSignup/Data/add.txt`
stores the number of lines some file has...
eg output:0 /usr/local/tomcat4/webapps/EmailSignup/Data/add.txt
so now i want to have an if statement that will compare to see if the number of lines are equal to zero...
how can i do that... this is what i do but it never seems to go in the if...
it always goes in the else..
if [ "$L" == " 0 /usr/local/tomcat4/webapps/EmailSignup/Data/add.txt" ] ;
then
echo $L
echo No additions to the Email List
else
echo --$L
echo Adding new entries to the Email List
fi
any ideas?
thanks
udamTry using the '-eq' operator of the 'test' program. (Square [] brackets in a Bourne shell script invoke the '/bin/test' program for your convenience. See the man page for 'test' for more info.)
'-eq' compares two values NUMERICALLY, while '==' compares two values as STRINGS.
Oops, I see what the other poster said about the leading space. What you really need to do is cut the number out of the string. You don't care about the file name.
if [ `echo "$L" | cut -c 1-7` -eq 0 ] ;
then
echo $L
echo No additions to the Email List
else
echo --$L
echo Adding new entries to the Email List
fi -
i cannot figure out how to get a button made in interface builder/xcode to run/execute a simple shell script. how do i do it. say i made a regular push button named button and i wanted it to start the "fivemin" shell script i pasted in below. assume the shell script is in my path already, do i need to export the path again as well.
also, my version of xcode is missing the shell script automator fron the new products options, is this a new thing.
but i would rather do it in a cocoa app. please give me an example say with a timed loop so i can check to see the process running via ps
###name: fivemin
#!/bin/bash
# this shell script simply sleeps in $incr second intervals for
# five minutes
trap 'echo "fivemin: EXITING"' EXIT
typeset -i nsecs=5*60 i=0 incr=4
while [ $i -lt $nsecs ]; do
sleep $incr
((i+=incr))
done
exit 0To wrap a unix tool you need to use NSTask. Here's a useful tutorial from CocoaDevCentral.
If you're new to Cocoa as you say (in your other post), then I'd first recommend working through a couple of the basic tutorials knocking around to get an idea of the language and the tools available. Apple's own venerable 'Currency Converter' tutorial is here. MacDevCenter's also got quite a few here. You'll be able to find others on the web.
My version of Xcode (2.1) has a 'Shell Script Automator Action' in the new project assistant, 'Action' section. I've no idea if this has changed in the latest versions, nor why yours might be missing it. The template used when creating it is located in /Application Support/Apple/Developer Tools/Project Templates/Action/Shell Script Automator Action/.
Another solution that you might consider is using AppleScript Studio. This would use the 'do shell script' command from AppleScript and would use Interface Builder to create your window and button in a similar manner to a Cocoa version. -
Problem with shell commands and scripts from an Applescript Application
Hi-
I am fairly new to OSX software development. I am trying to build an application that creates a reverse SSH tunnel to my computer and starts OSXvnc. The idea is that my Mom or anyone else who needs help can connect to me without having to tinker with their firewalls.
There are plenty of resources on how to do this, and I have found them. What I have a problem with is the following.
I am building this application in Xcode as an Applescript application, because Applescript and shell scripting are the only forms of programming I know. I use an expect script to connect through SSH, and a "do shell script" for the raw OSXvnc-server application to allow screen sharing.
The problem is that when I click on the button to launch OSXvnc-server or the button to launch SSH, the application freezes until the process it spawns is killed or finishes. For example, I can set SSH to timeout after 60 seconds of no connection, and then the Applescript application responds again. I even tried using the ssh -f command to fork the process, but that doesn't seem to help.
I am also using "try" around each of the items.
What am I doing wrong? How can I make the buttons in my app launch SSH and OSXvnc-server without hanging the application while it waits for them to finish?
Thanks so much!See here for an explanation of the syntax.
(20960) -
Problem with backtick replacing apostroph in applescript/shell script
I've got a script that appears to be using a backtick instead of an apostrophe which is causing an error in my shell script. For the life of me I can't seem to find where the error is being generated?
The script is attached below. I'm using Exiftool, an app that writes metadata to image files. The shell script
set cmd to "exiftool -CopyrightNotice=" & exifCopyright & " " & thisFilePath & ""
set theResult to do shell script cmd
works fine but the following shell script
set cmd to "exiftool" & space & authorStr & " " & thisFilePath & ""
set theResult to do shell script cmd
returns the error "sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file" number 2. The code in the event log in applescript editor looks exactly the same to me but one fails in the shell script.
It has been suggested by the developer of Exiftool, Phil Harvey, that there is a backtick in the second shell script. I read somewhere in the applescript docs that this is due to a change in OS 10.6? Any suggestions on how to fix this?
Thanks.
PedroYea, the authorStr value has a space like "Joe Smith"
Then you need to use quoted form of this string, too:
set cmd to "exiftool " & quoted form of authorStr & space & thisFilePath
although the format looks wrong to me - shouldn't there be some kind of switch, such as "-author=' before it?
You have to consider how you'd enter this at the command line to work out how best to translate it to AppleScript. For example, if the command line version were:
exiftool -author='John Doe' /path/to/some.jpg
you can see the quotes are around the name, not the entire -author switch. In this case you should be looking at something like:
set authorStr to "John Doe"
set theFilePath to "/path/to/some.jpg"
set theCmd to "exiftool -author=" & quoted form of authorStr & space & quoted form of theFilePath
Now you could, of course, use quoted form when you create the variables (e.g. set authorStr to quoted form of "John Doe"), but that may screw you up later on if/when you try to use authorStr in some other way, so I find it best to use quoted form only where it's needed. -
Shell script friendly paths in applescript
I have a python script that resides inside a standalone application bundle. I run this script from the app and call it by getting the path of the application bundle and adding '/Contents/Resources/' to the path. This script sets or gets information from a plist. So, adding a ' --list' to the script name returns the items of the plist.
This is my applescript and RWplist is the python script.
set plInfo to do shell script "/Users/[just me]/Desktop/Test Project/Test App.app/Contents/Resources/RWplist --list'
This fails due the the spaces in the directory and app name. If I change the folder and app name like;
set plInfo to do shell script "/Users/[just me]/Desktop/Test-Project/Test-App.app/Contents/Resources/RWplist --list'
This works... I tried wrapping the path in single quotes, I tried to do a replace all spaces to '\\space' in the path... and adding the 'quoted form of POSIX path of'...
I'm guessing I'm placing the single quotes or using the quoted form... improperly. Can someone please explain how to format a path in this manor. I can't seem to find the proper syntax for this.This fails due the the spaces in the directory and app name
Right, this is a common problem that many people encounter.
I'm guessing I'm placing the single quotes or using the quoted form... improperly. Can someone please explain how to format a path in this manor.
quoted form is the preferred/recommended way of doing this. Here's an example:
set cmdPath to "/Users/just me/Desktop/Test Project/Test App.app/Contents/Resources/RWplist"
do shell script (quoted form of cmdPath) & " --list"
If you want to build the script manually then you should single-quote the entire path, e.g.:
do shell script " '/Users/just me/Desktop/Test Project/Test App.app/Contents/Resources/RWplist' --list"
Note that there are single quotes around the command path but the --list parameter is outside of the single-quoted path. -
Find & replace part of a string in Numbers using do shell script in AppleScript
Hello,
I would like to set a search-pattern with a wildcard in Applescript to find - for example - the pattern 'Table 1::$*$4' for use in a 'Search & Replace script'
The dollar signs '$' seem to be a bit of problem (refers to fixed values in Numbers & to variables in Shell ...)
Could anyone hand me a solution to this problem?
The end-goal - for now - would be to change the reference to a row-number in a lot of cells (number '4' in the pattern above should finally be replaced by 5, 6, 7, ...)
Thx.Hi,
Here's how to do that:
try
tell application "Numbers" to tell front document to tell active sheet
tell (first table whose selection range's class is range)
set sr to selection range
set f to text returned of (display dialog "Find this in selected cells in Numbers " default answer "" with title "Find-Replace Step 1" buttons {"Cancel", "Next"})
if f = "" then return
set r to text returned of (display dialog "Replace '" & f & "' with " default answer f with title "Find-Replace Step 2")
set {f, r} to my escapeForSED(f, r) -- escape some chars, create back reference for sed
set tc to count cells of sr
tell sr to repeat with i from 1 to tc
tell (cell i) to try
set oVal to formula
if oVal is not missing value then set value to (my find_replace(oVal, f, r))
end try
end repeat
end tell
end tell
on error number n
if n = -128 then return
display dialog "Did you select cells?" buttons {"cancel"} with title "Oops!"
end try
on find_replace(t, f, r)
do shell script "/usr/bin/sed 's~" & f & "~" & r & "~g' <<< " & (quoted form of t)
end find_replace
on escapeForSED(f, r)
set tid to text item delimiters
set text item delimiters to "*" -- the wildcard
set tc1 to count (text items of f)
set tc2 to count (text items of r)
set text item delimiters to tid
if (tc1 - tc2) < 0 then
display alert "The number of wildcard in the replacement string must be equal or less than the number of wildcard in the search string."
error -128
end if
-- escape search string, and create back reference for each wildcard (the wildcard is a dot in sed) --> \\(.\\)
set f to do shell script "/usr/bin/sed -e 's/[]~$.^|[]/\\\\&/g;s/\\*/\\\\(.\\\\)/g' <<<" & quoted form of f
-- escape the replacement string, Perl replace wildcard by two backslash and an incremented integer, to get the back reference --> \\1 \\2
return {f, (do shell script "/usr/bin/sed -e 's/[]~$.^|[]/\\\\&/g' | /usr/bin/perl -pe '$n=1;s/\\*/\"\\\\\" . $n++/ge'<<<" & (quoted form of r))}
end escapeForSED
For what you want to do, you must have the wildcard in the same position in both string. --> find "Table 1::$*$3", replace "Table 1::$*$4"
Important, you can use no wildcard in both (the search string and the replacement string) or you can use any wildcard in the search string with no wildcard in the replacement string).
But, the number of wildcard in the replacement string must be equal or less than the number of wildcard in the search string. -
Applescript: display dialog while doing shell script
Hello there,
I'm making an applescript app for my company, and had a question.
The functionallity of the app is working great, but there is a certain step which can take up to several minutes.
This can give the user the feeling that nothing is happening and things are stuck.
Is there a possibility to display a dialog as long as the action (shell script) is running?
Something along the lines of "Now performing action X. please wait...)
Thanks for your thoughts!
GrtzWith regular AppleScript you can start the shell script in the background (see do shell script in AppleScript) and then put up a dialog, although you would have to periodically check to see if the shell script is finished. In Lion, the AppleScript Editor has a Cocoa-AppleScript template that you can use (kind of a wrapper application that lets you use various Cocoa methods without having to use Xcode), in which case you could put up an indeterminite progress indicator and then do the shell script.
AppleScript Studio is deprecated as of Snow Leopard, but there are some AppleScriptObjC in Xcode tutorials at MacScripter. An additional source of information and templates is macosxautomation, but they seem to be having some server problems at this time. I also have a Progress Window template application example, it can be downloaded here.
Maybe you are looking for
-
I was running CS2 with no problems until I got photoshop elements 8.0. It gave me an error every thime I tried to open it so I deleted it. Now I tried to reinstall it and when I click on install the program just closes down. What do I need to do to g
-
How to Wirelessly sync and connect my iPhone 5 to itunes?
Hey i just wanna no if there is a way to sync iphone and contacts and trasfer songs and delete songs ect wirelessly to my mac itunes. just everything the same but without the cable Thanks
-
How to disable popup from Java for upate of J2SE?
Dear We have an instance 12.0.5 running on AIX box.Since last week whenever user tries to login & use ERP, java popup appears. It asks whether to download J2SE update. Once we accept & download, it gets connected to intenet (java.com) for verificatio
-
i have no before owner to active my mini i pad . when i format ipad then i face a apple account . but know at this time no owner of the apple account . how i can fix it .... pls tell us
-
Need help on MS8808 video card
I have a Vanta / TNT2M64 video card (MS8808 Ver 1A) purchased from Techtronics here in western Canada. I accidentally shorted the card against my metal computer case while re-installing the card ..and now its dead. I put the old video card back in my