Automator Apple Script to convert jpeg image - applescript

I used below automator function to convert png image to jpeg in bulk. It worked fine and converted all images in folder at once. But quality of jpeg image is not full.
Image 1: Automator Functions
Image 2: Preview App shows quality of image. How can we do same in automator above function ?

Unfortunately this functionality is just not available in that Action (weirdly). I would suggest using the "Run Shell Script" Action.
[EDIT: I've replaced everything here with new graphic and thorough explanation.]
Explanation:
In the red box:
These are the Actions. There are 5. There are 2 Run Shell Script Actions, one directly following the other.
In the green box:
These are the details for the Actions.
1: Ask for Finder Items:
Type: Folders
Do NOT Allow Multiple Selection
2: Get Folder Contents:
You may use "Repeat for each subfolder found" if you wish
3: Run Shell Script:
Pass Input: as arguments
The script:
for f in "$#"
do
sips -s format jpeg -s formatOptions 100 "$f" --out "${f%.png}.jpg";
#rm "$f";
echo "${f%.png}.jpg"
done
4: Run Shell Script:
Pass Input: as arguments
The script:
for f in "$#"
do
echo "$f" | grep .jpg
done
5: Move Finder Items:
To: [select your folder]
NOTE: To be safe, I have commented out the "rm" line in the first Run Shell Script script (put a "#" in front of it) for testing. This is the line that deletes the PNG files. If everything works after testing, you can remove the "#".

Hello you may have to install ImageMagick, since I am not sure you can write an AppleScript or something to make Preview use a better quality, and I can't finde any user preferences setting for quality in the user defaults database. It may exist, but I haven't found it.
Here is a link with some info on ImageMagick: Command-line Tools: Convert I think your best choice is to use an execute unix script action, after having converted the paths from finder, or the folder from finder, and have a shell script inside the action, that performs the converstion with the desired quality for you.
New (for the case that it is impossible to make CRGreens solution to work, see comments below)
We will need to have the folder where your images that are to be converted to along the way, so I suggest you put the AppleScript below into a "run applescript action" in a new workflow, then select the folder where the conversion occur in finder, before you run the workflow you just created, then hit "Cmd-V" in a new TextEdit document, so you have it for later.
on run {input, parameters}
tell application "Finder"
if selection is not {} then
set theSel to selection as alias list
end if
end tell
if theSel is not {} then
set theFol to item 1 of theSel
set the clipboard to "cd " & quoted form of POSIX path of theFol
else
beep
end if
end run
Save and close the Automator workflow, in case you'll need it on another occation.

Related

MacOS Terminal: Is it possible to create a "drag-and-drop" action via script?

There are many posts that explain how to drag-and-drop things into an open Terminal window. But what I would like to do is to tell Terminal to drag-and-drop a previously selected directory onto another application like VSCode or Renamer. I have not found any documentation for that. Is it at all possible? And if so, would somebody please point me to a documentation?
UPDATE:
I'd like to clarify my question with what I intend to do:
Pre requisites:
a "work folder" contains folders and files that shall be renamed
the renaming is done by an application called "A better finder renamer" (which allows presets)
An "Automator" (MacOS app) action shall imitate these steps:
the "work folder" is right clicked
the folder is drag-and-dropped onto the ABFR, which initiates the currently active preset
other actions via bash (like 'mv .//.* ./') ...
It is the "drag-and-drop" part of the Automator action that presents a riddle for me.
The "drag-and-drop" operation is manual operation. In AppleScript, instead the command to open the file or folder is given to the destination application itself.
Second thing to keep in mind. Getting Terminal's current working directory is easy with its do script "pwd" command. But the result of the do script Terminal command returned to the script is always the window tab, not the result of the pwd shell command. One solution is to redirect (remember) the result of pwd in a temporary text file.
set tempFolder to (path to temporary items folder from user domain)
set temp to POSIX path of tempFolder & "workingDirectory.txt"
tell application "Terminal" to do script ("pwd > " & temp) in selected tab of window 1
set curDirPosixPath to paragraph 1 of (read file ((tempFolder as text) & "workingDirectory.txt"))
set curDirHFSPath to curDirPosixPath as POSIX file as Unicode text
tell application "Visual Studio Code" to open curDirHFSPath
.
NOTE: other possible solution (I don't like) is parsing the text contents of Terminal window after pwd command execution. You can get contents using property contents (of selected tab of front window).
Open Automator, choose create New Document.
Choose create new Quick Action (service).
Set workflow receives current folders in any application.
From library Add Run AppleScript action. Edit it contents:
on run {input, parameters}
set curDirHFSPath to (item 1 of input) as text
tell application "Visual Studio Code" to open curDirHFSPath
end run
Save this Quick Action as service. Now when right-clicking the folder, opens the context menu, where you can choose and run this service.

how to write shell script of "removing watermark and ocr" in automator quick action?

I'm using despeck software for removing watermark and extrect txt from image and pdf file. the source installation source is--> https://github.com/despeck/despeck
I have tested all despeck features like remove watermark, OCR, extract text from pdf. Now my concern is how I implement these features in Automator for quick action. it's taking too much time to remove the watermark or OCR for bulk operation. so I want only one click operation using Automator quick action functionality. can you provide a shell script for removing the watermark, OCR, and extract text from pdf, actually I'm facing the problem "how to tell "what is the file name and how to assign output name in a shell script" I have tried but an error show "to many arguments" check this shell script code.....
source ~/.bash_profile
for f in "$#"
do
echo "Removing Watermark: $FILE"
EXT=${FILE##.}
despeck remove "$FILE" -o "${FILE/%.$EXT/.}"
done
Automator Code part 1
automator code part 2
Run automator quick action 1
after running automator quick action

Run Bash script on selected files in Finder

I have a tiny Bash script that executes ffmpeg and a touch command on an input file. I use this to recompress video files from my camera. I would like to be able to right-click files in Finder and run the script on the select file(s), preferably showing the terminal window while executing and closing when done.
How to do this on macOS?
I think this is what you want. I started Automator by pressing ⌘space and starting to type "Automator", hitting ↩ as soon as it guessed correctly. I then created a "Quick Action" that contains this code:
on run {input, parameters}
repeat with theItem in input
set f to POSIX path of theItem
tell application "Terminal"
activate
tell window 1
do script "echo " & f
end tell
end tell
end repeat
end run
and looks like this:
It basically just echos the filename, but you can put ffmpeg commands in there instead.
Why using finder? Or automator? Or going though loops and hoops just to use the GUI?
You have fully-functional bash shell in MacOS, so save time and hassle with the below one-liner.
Assuming you need to run your script for all *.mpeg files in the folder.
Try this:
ls *mpeg | xargs <your_script_name>
You will see the execution output in the same terminal window.

Run Automator Workflow in background

Trying to make Automator workflow that uses "Convert to TXT Document" for pdf to txt convertion. But while running, Abbyy FineReader window becomes active. Is it possible to run in in silent mode or with minimized window?
This AppleScript works for me using the latest version of Sierra. Tested on my system, it did not bring Abbyy FineReader to the foreground.
set thePDF to (choose file)
tell application "FineReader"
set resultFile to export to txt thePDF ¬
from file thePDF
end tell
Your new text file should appear in the same directory as your original PDF
I'm not using Automator so I do not know which method you are going to use to pass the PDF file to this AppleScript. For testing purposes, I used to the “choose file” command. If you are using Automator to pass the PDF file that you specified in a previous Automator action, you can just remove the “choose file” command from the code. Anyway, all you need to do is add a “run AppleScript” command in your Automator workflow.
If you are going to remove the “choose file” command, you'll need to re-define the value for the variable thePDF
NOTE FineReader actually has an extensive AppleScript dictionary. My answer included a minimal version of many other options for the exporting as text. Here is a full version example of options
tell application "FineReader"
set resultFile to export to txt directParamFile ¬
from file fromFileFile ¬
ocr languages enum ocrLanguagesEnumLanguageListType ¬
saving type savingTypeSaveSettingsEnum ¬
retain layout retainLayoutTxtLayout ¬
keep page numbers headers and footers keepPageNumbersHeadersAndFootersBoolean ¬
keep line breaks and hyphenation keepLineBreaksAndHyphenationBoolean ¬
insert page break character as page separator insertPageBreakCharacterAsPageSeparatorBoolean ¬
use blank lines useBlankLinesBoolean ¬
encoding encodingEncodingEnum
end tell
I decided not to use FineReader applet. Instead I migrate to stack: tesseract + ImageMagick + gs.
If anybody is interested, I attach my sollution below.
Automator shell script
export PATH=/usr/local/bin:$PATH
/usr/local/bin/convert -density 300 "$#" -depth 8 -strip -background white -alpha off image.tiff
/usr/local/bin/tesseract -l rus image.tiff ~/Desktop/OCR
rm image.tiff
And the
Automator workflow
You could try this applescript in the script editor, replacing the file paths to your files. I don't have the program installed, so I haven't tested it. If it doesn't work, maybe it's something you can build on to get the result you're after.
tell application "FineReader" activate
tell application "System Events" set visible of process "FineReader" to false
tell application "FineReader"
export to txt "/Path/to/filename/File_to_OCR.pdf" from file "/Path/to/filename/File_to_OCR.pdf"
end tell

Applescript to Save File as Two Separate File Formats with Fixed Extensions

I often need to save a screenshot of an art file as two different files. The first would be a PDF with a "_prv" at the end of the file name. The second would be a JPG with an "_thm" file name.
So, examples of file names would be:
1733419_prv.pdf
1733419_thm.jpg
The "1733419" part would be job-specific, and it would be nice if the script would prompt the user the enter that information. It would also be nice if the script could prompt the user for the location that the new files should be saved to.
Is this all possible as an applescript or possibly a Photoshop script?
Thank you so much for any help!
Bryan
Updated Answer
I have improved the script a little. It will now do nothing and exit gracefully if you select Cancel when entering the Job Reference number. It will also now handle spaces in the Job Reference number. It also creates an output directory on your Desktop, so if you enter a Job Reference number of 1234 you will see a directory (folder) called 1234 appear on your Desktop which contains the 2 output files.
#!/bin/bash
# Ask user for job reference, store in $ref
ref=$(osascript -e 'Tell application "System Events" to display dialog "Enter Job Reference:" default answer ""' -e 'text returned of result' 2>/dev/null)
# Quit without doing anything if user didn't enter anything
[ -z "$ref" ] && exit
# Make a place for the output files
DIRECTORY="$HOME/Desktop/$ref"
mkdir "$DIRECTORY" 2> /dev/null # Don't worry if it already exists
cd "$DIRECTORY"
# Get user to select area to grab, then save as JPEG
screencapture -i -t jpg "${ref}_thm.jpg"
# Convert JPEG to PDF
sips -s format pdf "${ref}_thm.jpg" --out "${ref}_prv.pdf" > /dev/null 2>&1
Original Answer
I hope this is close to what you mean. Save it in your HOME directory as grab, then start Terminal and type:
chmod +x grab
which will make the file executable.
#!/bin/bash
# Ask user for job reference, store in $ref
ref=$(osascript -e 'Tell application "System Events" to display dialog "Enter Job Reference:" default answer ""' -e 'text returned of result' 2>/dev/null)
# Get user to click on a window to grab, then save as JPG
screencapture -w -i -t jpg ${ref}_thm.jpg
# Convert JPG to PDF
sips -s format pdf ${ref}_thm.jpg --out ${ref}_prv.pdf > /dev/null 2>&1
You can now either type:
./grab
or double-click on the file called grab in the Finder.
It will ask you for a job reference and then start the screen grab. You should click on a window to tell it which one you mean. It will then create the two files you asked for.
The advantage of this method is that no extra software is required to be installed - such as ImageMagick or anything since it uses built-in OSX tools only.

Resources