I'm using Mach Desktop to echo a script's results into a Desklet, so it has to be in this format:
echo 'tell application "iTunes" to return album of current track' | osascript
I have a single line in which to enter data, so the script needs to be one long string.
It may be that the entire thing must be contained within one set of single quotes after echo, as follows:
echo '[entire script]' | osascript
How do I convert the AppleScript below to one line?
set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias
set the_text to read some_file as string
set the text item delimiters of AppleScript to ", "
set the_lines to (every text item of the_text)
return some item of the_lines
Here is what I'm trying:
echo 'set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias -e set the_text to read some_file as string -e set the text item delimiters of AppleScript to ", " -e set the_lines to (every text item of the_text) -e return some item of the_lines' | osascript
And that gives this error:
107:112: syntax error: A "set" can't go after this identifier. (-2740)
I suppose you could try out this script. I'm confused as to what you're actually trying to accomplish, though, so I apologize if this script is unsatisfactory.
echo 'set some_file to "~/Library/Application Support/Notational Data/Words.txt" as POSIX file as alias' -e 'set the_text to read some_file' -e 'set the text item delimeters of AppleScript to ","' -e 'set the lines to (every text item of the_text)' -e 'return the lines' | osascript
EDIT: #Zade That would be because Words.txt doesn't exist. Aliases only refer to existing files. Therefore, you must choose a file that exists. Here is an easy way to see what the correct syntax is for file references:
set some_file to (choose file)
You will notice that the file path is delimited by colons ( : ) rather than slashes. Having said this, choose a file that exists, and run your script with that file.
Related
So I'm trying to run multiple Applescript commands from the command line in one go. However, no matter how I try it, it won't work:
$ osascript -e "set x to 0; display dialog x"
$ osascript -e "set x to 0 \n display dialog x"
$ osascript -e "set x to 0 then display dialog x"
Is there a way to do this without saving to file?
This works for me:
osascript -e "set x to 0" -e "display dialog x"
Have a look at the -e option in the manual page for osascript in Terminal: man osascript
−e statement
Enter one line of a script. If −e is given, osascript will not look for a filename in the argument list. Multiple −e options may be given to build up a multi-line script. Because most scripts use char- acters that are special to many shell programs (for example, AppleScript uses single and double quote marks, “(”, “)”, and “∗”), the statement will have to be correctly quoted and escaped to get it past the shell intact.
You can also do e.g.:
osascript <<END
set x to 0
display dialog x
END
Or:
osascript -e '
set x to 0
display dialog x'
Your second attempt was very close to proper:
osascript -e "set x to 0
PRESS ENTER
display dialog x"
PRESS ENTER
When I copy multiple paragraphs of data like
line 1
line 2
line 3
to my clipboard on Mac I can access its elements via AppleScript through
on run {input, parameters}
set theClip to input as text
set value of variable "Empfänger" of front workflow to paragraph 1 of theClip
set value of variable "Betreff" of front workflow to paragraph 2 of theClip
set value of variable "Textkörper" of front workflow to paragraph 3 of theClip
end run
and write it to Automator variables. Can I do the same thing in Shell-Script? When I run
for f in "$#"
do
echo "$f"
done
it seems like everything is stored in $1.
Actually, I wouldn't mind to use paragraphs as separator but a configurable sign like {NEXT} or something similar.
Thank you in advance!
Your example seems to be happy to treat a line as a paragraph. So, I'll do the same. So, if you copy your three lines of sample data into your clipboard by selecting them below and pressing ⌘C:
line1
line2
line3
and you want to separate them into shell variables, as you say:
para1=$(pbpaste | sed -ne '1p')
and check:
echo "$para1"
line 1
Likewise:
para2=$(pbpaste | sed -ne '2p')
para3=$(pbpaste | sed -ne '3p')
Or, if you mean you want the lines in an array:
paras=( $(pbpaste) )
echo ${paras[0]}
line1
echo ${paras[1]}
line2
Or, if you want loop over the elements:
for p in "${paras[#]}" ; do echo $p; done
I use a Mac (osx sierra) and I have been learning how to use the bash. I am trying new stuff as I go along to grasp some concepts. This time I was experimenting with functions and aliases. But then I couldn't wrap my head around this problem:
I first echoed this function into the .profile to insert new aliases and functions easily into the .profile file.
function editprofile(){
echo "$#" >> ~/.profile
}
This function worked very well for some alias insertions. But after I tried to insert an alias for the script (the script below) that makes my mac sleep, I realized that the function creates some diffuculties with the cascading single and double quotes. I believe this will be the case for most scripts that uses lots of layers of single and double quotes.
osascript -e 'tell application "Finder" to sleep' && exit
The code below is me trying to use my function to insert the code above as an alias into the .profile.
editprofile 'alias _sleep=' "'" 'osascript -e' "'" 'tell application "Finder" to sleep' "'" '&& exit' "'"
The problem is that when the second script is echoed into the .profile file, I should still keep some escape characters, otherwise the code is interpretted by the bash incorrectly. I think this would also be the case with may other scripts that have this many layers of quotes, so I though I should ask if there is any way around.
P.S.
On a related note, it seems like when I type this:
function editprofile(){echo "$#" >> ~/.profile}
instead of this:
function editprofile(){
echo "$#" >> ~/.profile
}
into the .profile file, the script doesn't work. Is it because of the line breaks?
Assuming that you have a function, as opposed to an alias:
_sleep() { osascript -e 'tell application "Finder" to sleep' && exit; }
...you can emit its text with declare -f. Thus:
declare -f _sleep >>~/.profile
...or, to use your existing editprofile function:
editprofile "$(declare -f _sleep)"
The easiest approach is just that: Define the function in your local shell, then have the shell itself do the work of emitting it -- and quote that emitted content so it doesn't get field-split into individual arguments (and then have those arguments individually evaluated as globs).
If you don't want to go that route, there are approaches available; they're just varying degrees of unpleasant.
printf -v cmd_var '%q ' osascript -e 'tell application "Finder" to sleep'
...will put correctly-quoted contents into "$cmd_var". You could then:
printf -v sleep_def '_sleep() { %s && exit; }' "$cmd_var"
...which will give you a function declaration in sleep_dev that can be evaled to execute it locally, or appended to your .profile, &c.
editprofile "$sleep_def"
...will behave appropriately in that context.
I'm reading the scripts from here and trying to understand what's going on. This function performs changing the directory of a Finder window:
function ee {
osascript -e 'set cwd to do shell script "pwd"'\
-e 'tell application "Finder"'\
-e "if (${1-1} <= (count Finder windows)) then"\
-e "set the target of window ${1-1} to (POSIX file cwd) as string"\
-e 'else' -e "open (POSIX file cwd) as string"\
-e 'end if' -e 'end tell';\
};\
I'm assuming the $ is interpreted by bash, since it's inside double-quotes. I haven't been able to find what could {1-1} mean. I've played with the expression in separate test scripts but couldn't find a difference from plain $1. Any ideas?
This means that if argument 1 (${1}) is not set, it will be set to 1.
See parameter substitution here.
${parameter-default}, ${parameter:-default}
If parameter not set, use default.
I know I can write return to "path to file", but I would like to know if there is a way to write the backspace character to a file so as to delete a character without using TextEdit, just the write command. I've tried backspace, bckspc and even delete, but none of them seem to be working. Any help would be appreciated.
This will delete the last character of the file "test" located on the desktop:
set myFile to (path to desktop as text) & "test"
set fRef to (open for access file myFile with write permission)
try
set eof of fRef to (get eof of fRef) - 1
end try
close access fRef
This would work even if the file didn't end with an ASCII character:
set f to POSIX file ((system attribute "HOME") & "/Desktop/test.txt")
set input to read f as «class utf8»
if input is not "" then set input to text 1 thru -2 of input
set b to open for access f with write permission
set eof b to 0
write input to b as «class utf8»
close access b
You could also use something like this:
shopt -u xpg_echo
x=aä
echo -n "$x" > test.txt
x=$(cat test.txt)
x=${x%?}
echo -n "$x" > test.txt
cat test.txt
rm test.txt