The argument for osascript -e
can contain newlines:
osascript -e 'set x to "a"
say x'
You can also specify multiple -e
arguments:
osascript -e 'set x to "a"' -e 'say x'
Or if you use a heredoc, bash
interprets three characters (\
, $
, and `
) between <<END
and END
but no characters between <<'END'
and END
.
osascript <<'END'
set x to "a"
say x
END
Edit:
Since osascript can operate with a heredoc (ie take input from /dev/stdin) then one can just write the script as a whole file and prepend with the correct shebang line:
#!/usr/bin/env osascript
set x to "a"
say x
This also allows you to save your apple script as a actual program in ~/Applications/.app using the following procedure (changing for your script's name):
mkdir -p ~/Applications/<APP_NAME>.app/Contents/MacOS
touch ~/Applications/<APP_NAME>.app/Contents/MacOS/<APP_NAME>
open -A TextEdit ~/Applications/<APP_NAME>.app/Contents/MacOS/<APP_NAME>
Ensure that both the script file in .../MacOS/ and the matches
The great thing about UNIX commands is that you can combine them together. iconv
doesn't know how to recurse into directories, but find
does. It can call iconv
on every file it sees.
(These commands will convert all files in the current directory and all directories within. Make sure you are in the directory you want to convert all files in recursively.)
To change all files with the extension .txt
:
find . -type f -name '*.txt' -print0 |
while IFS= read -r -d $'\0' filename; do
iconv -f utf-8 -t utf-8 -c "$filename" > "$filename".iconv_cleaned_utf8
mv "$filename".iconv_cleaned_utf8 "$filename"
done
I suppose this code requires some explanation. What it does is:
find
prints out all the filenames of the files involved, separated by a null byte (the null byte is the only invalid character for a file path)
- bash
read
s the filenames and loops through them
iconv
converts the file to a tempfile with an extra extension
- we
mv
the tempfile to replace the original file.
If they have different extensions (this is for any and all files under the current directory), remove the -name *.txt
It's a bit cleaner if you have the sponge
utility from moreutils
, but that is not installed by default.
find . -type f -name '*.txt' -print0 |
while IFS= read -r -d $'\0' filename; do
iconv -f utf-8 -t utf-8 -c "$filename" | sponge "$filename"
done
Best Answer
Bash can figure out where the last underscore is using a regular expression match, then you can use its substring extraction to get the part of the filename that you care about. Something like this: