How to pass multiple arguments to osascript? - macos

I've got the following shell script which uses osascript command:
#!/usr/bin/osascript
on run argv
tell application "Terminal"
activate
do script "echo " & quoted form of (item 1 of argv) & " " & quoted form of (item 2 of argv)
end tell
end run
However when I run, the code is only limited to print 2 first arguments.
E.g. when running ./test.sh foo bar buzz ..., I expect all the arguments to be displayed.
How I can convert the above code to support multiple unlimited number of arguments? And it won't break when I specify none?

By default, AppleScript's text item delimiters is {} and unless is was set to other then the default elsewhere in the script before this and not reset as one should directly after the manipulation, and or you just are not using AppleScripts's text item delimiters, then here is a way to do it without having to explicitly use code like e.g. set {TID, text item delimiters} to {text item delimiters, space} and set text item delimiters to TID:
#!/usr/bin/osascript
on run argv
set argList to {}
repeat with arg in argv
set end of argList to quoted form of arg & space
end repeat
tell application "Terminal"
activate
do script "echo " & argList as string
end tell
end run

You have to add a repeat loop to map the argument list to their quoted form and then join the list to a space separated string with text item delimiters
#!/usr/bin/osascript
on run argv
set argList to {}
repeat with arg in argv
set end of argList to quoted form of arg
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set argList to argList as text
set text item delimiters to TID
tell application "Terminal"
activate
do script "echo " & argList
end tell
end run

Related

Removing part of string in Applescript

So I have a string but I only want it after a certain part. I know that you would optimally do this in shell so I tried, but my limited shell knowledge didn't bring me far. This post tries to do a similar thing but not exactly what I need.
set theString to "hello/world"
do shell script "echo " & theString & "???"
return theString --the output should be hello
There's no shell script needed. Get the position of the slash in the string and return the substring from the beginning to the position - 1
set theString to "hello/world"
set slashIndex to offset of "/" in theString
return text 1 thru (slashIndex - 1) of theString
You can also use AppleScript's text item delimiters:
set theString to "hello/world"
set {TID, AppleScript's text item delimiters} to ¬
{AppleScript's text item delimiters, "/"}
set theString to first text item of theString
set AppleScript's text item delimiters to TID
do shell script "echo " & theString's quoted form & "???"
Returns:
hello???
However, return theString after processing with AppleScript's text item delimiters will just return hello in this use case.
Note the use of 's quoted form in theString's quoted form with the variable in the do shell script command, as you should always quote what's being passed to the shell. You can also use this form: quoted form of theString
As you can see the offset of method presented in one of the other answers is more straight forward, however I've added this as an answer so you know what your other options are.
This also works.
set theString to "hello/world"
set firstWord to 1st word of theString
return firstWord
OR
If there are any other characters and including “/“ in theString , the following should take care of that.
set theString to "hello/world"
set firstWord to 1st word of (do shell script "echo " & ¬
quoted form of theString & " | sed -E 's#[^[:alpha:]]{1,}# #'")
return firstWord

Applescript (or automator action) to convert HTML to Plain Text

I had an Applescript or Automator action that let me select files and then it would convert the ones that were HTML to plain text. Somehow I may have lost that script.
I think the script was the one below, but when I use it and select some files, it converts some of them to .rtf and then they disappear. Bizarre.
Any help would be greatly appreciated.
set ASTID to AppleScript's text item delimiters
choose file with multiple selections allowed without invisibles
repeat with thisFile in result
try
set thisFile to POSIX path of thisFile
do shell script "/usr/bin/textutil -convert rtf " & quoted form of result
set AppleScript's text item delimiters to {"."}
set thisFile to text items 1 through -2 of thisFile
set AppleScript's text item delimiters to {""}
set thisFile to thisFile as text
do shell script "/System/Library/Printers/Libraries/convert" & ¬
" -f " & quoted form of (result & ".rtf") & ¬
" -o " & quoted form of (result & ".pdf") & ¬
"; /bin/rm -f " & quoted form of (result & ".rtf")
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try
end repeat
set AppleScript's text item delimiters to ASTID

Sub-Routine POSIX path issue.

Working on a script that downloads files and places them in a folder for for automated installation. This is the first time I've created an "on" sub-routine and I'm having issues with resolving the path to check if the file exists. The process uses a Terminal window to monitor the download status.
set theContentPath to POSIX path of (choose folder)
set toInstall to (theContentPath & "ToInstall/")
set inStalled to (theContentPath & "Installed/")
set theTemp to (theContentPath & "theTemp/")
do shell script "mkdir -p " & quoted form of toInstall
do shell script "mkdir -p " & quoted form of inStalled
do shell script "mkdir -p " & quoted form of theTemp
set theList to {"http://url1.pkg",
"http://url2.pkg",
"http://url3.pkg",
"http://url4.pkg",
"http://url5.pkg",
"http://url6.pkg"}
repeat with x from 1 to (count theList)
--display dialog item x of theList
set thisOne to item x of theList
getFileIfNeeded(thisOne, toInstall, theTemp)
end repeat
on getFileIfNeeded(theFileToGet, whereToSave, tempFiles)
set AppleScript's text item delimiters to "/"
set theItems to text items of theFileToGet
set theFile to item 5 of theItems
set theFileCheck to whereToSave & theFile
set theURL to quoted form of theFileToGet
tell application "Finder"
if not (exists file theFileCheck) then
tell application "Terminal"
if (count of windows) is 0 then
activate
end if
do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window
do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window
end tell
end if
end tell
end getFileIfNeeded
Tried adding the following set theFileCheck2 to (POSIX file theFileCheck) as string but the results are not what I expected as the file still get's downloaded.
Here's the routine with my attempt to get the path right.
on getFileIfNeeded(theFileToGet, whereToSave, tempFiles)
set AppleScript's text item delimiters to "/"
set theItems to text items of theFileToGet
set theFile to item 5 of theItems
set theFileCheck to whereToSave & theFile
set theURL to quoted form of theFileToGet
tell application "Finder"
set theFileCheck2 to (POSIX file theFileCheck) as string
if not (exists file theFileCheck2) then
tell application "Terminal"
if (count of windows) is 0 then
activate
end if
do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window
do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window
end tell
end if
end tell
end getFileIfNeeded
This should fix it.
It targets "System Events" instead of "Finder", a lot of the functionality previously in the Finders dictionary has been shifted to System Events over the last several years.
Also i simplified the part that extracts theFile, you can use the 'last' keyword in this case instead of a hard coded number.
on getFileIfNeeded(theFileToGet, whereToSave, tempFiles)
set AppleScript's text item delimiters to "/"
set theFile to the last text item of theFileToGet
set theFileCheck to whereToSave & theFile
set theURL to quoted form of theFileToGet
tell application "System Events"
if not (exists file theFileCheck) then
tell application "Terminal"
if (count of windows) is 0 then
activate
end if
do script "cd " & quoted form of tempFiles & " ; curl -O " & theURL in front window
do script "mv " & quoted form of tempFiles & "*.pkg " & quoted form of whereToSave in front window
end tell
end if
end tell
end getFileIfNeeded

Applescript to add grandparent folder+parent folder prefix to filename

I have multiple folders with sub folders that have files in them that need to be labeled with their parent folder+grandparent folder name.
i.e. Folder 1>Folder 2>File.jpg needs to be renamed to Folder_1_Folder_2_File.jpg
I was able to find a script that somewhat does it, and have been trying to reverse engineer it, but am not having any luck. The script below presents two challenges, 1) It includes the entire path from the root directory, and two, it deletes the name of the file, therefore only allowing one file to be renamed before it errors out. I know that the problem is that the script is renaming the entire file, I just don't know how to proceed.
tell application "Finder"
set a to every folder of (choose folder)
repeat with aa in a
set Base_Name to my MakeBase(aa as string)
set all_files to (every file in aa)
repeat with ff in all_files
set ff's name to (Base_Name & "." & (ff's name extension))
end repeat
end repeat
end tell
to MakeBase(txt)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set new_Name_Raw to every text item of txt
set AppleScript's text item delimiters to "_"
set final_Name to every text item of new_Name_Raw as text
set AppleScript's text item delimiters to astid
return final_Name
end MakeBase
Thank you!
tell application "Finder"
repeat with theItem in (the selection as list)
set theItem's name to (theItem's container's container's name) & "_" & (theItem's container's name) & "_" & (theItem's name)
end repeat
end tell
If you want to learn how AppleScript can work with an app, look through the app's dictionary of AppleScript commands (AppleScript Editor > File > Open Dictionary...).
Edit 1
Here's a version in which you select the "grandparent folder" containing folders containing the items to rename:
tell application "Finder"
set itemsToRename to {}
set selectedFolders to (the selection as list)
repeat with selectedFolder in selectedFolders
set childFolders to every item of selectedFolder
repeat with childFolder in childFolders
set grandchildItems to every item of childFolder
set itemsToRename to itemsToRename & grandchildItems
end repeat
end repeat
repeat with theItem in itemsToRename
set theItem's name to (theItem's container's container's name) & "_" & (theItem's container's name) & "_" & (theItem's name)
end repeat
end tell
Try:
set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
repeat with aFile in myFiles
tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
end repeat

How to get the Color profile of an Image using Applescript?

)
I want to write an apple-script which collect the color profile of an image.. can anyone please help me out how to do this? I've no idea!!
Thanks in advance.
I like using ExifTool by Phil Harvey to extract metadata. Here is a service I wrote to access the metadata quickly.
on run {input, parameters}
-- creates a metadata folder in the Documents folder to store results
set thePath to POSIX path of (path to documents folder) & "metadata" & "/"
do shell script "mkdir -p " & quoted form of POSIX path of thePath
set {inputFiles, outputFiles} to {{}, {}}
repeat with anItem in input
set end of inputFiles to quoted form of POSIX path of (anItem as text)
tell application "Finder" to set {name:fileName, name extension:nameExtension} to anItem
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
set end of outputFiles to quoted form of (thePath & baseName & ".txt")
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set {inputFiles, outputFiles} to {(inputFiles as text), (outputFiles as text)}
set text item delimiters to TID
do shell script "exiftool -a " & inputFiles & " -w " & quoted form of (thePath & "%f.txt" as text) & "; open " & outputFiles
end run

Resources