Run Automator Workflow in background - applescript

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

Related

How to open an EMACS file in OS X by double-clicking on it, using the `emacsclient` command?

I want to open an org-mode file selected in the Finder, by double clicking on it. But since I use Emacs in daemon-mode, I want to use the emacsclient command for that.
Thus the primary idea was to wrap the command emacsclient -c posixPathToFile in an AppleScript App to open it.
tell application "Finder"
set fileAlias to the selection as alias
set fileName to name of fileAlias
set posixPath to POSIX path of fileAlias
end tell
-- tell application "Emacs" to activate
try
do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
end try
I know some set commands are not needed. Let's assume this script is saved as Xemacs.app and that I associate this app to always open .org file.
Using this App does not work by double-clicking on the file, but rather if I select the file in the Finder and then call the Xemacs.app independently. Why ? I'm not confident enough with AppleScript to figure out what happens.
So the workaround was to use the Automator service
on run {input, parameters}
set posixPath to POSIX path of input
tell application "iTerm" to do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
return input
end run
The service is saved as 'Open in Emacs'
Now selecting a file and right-clicking and callig Service > "Open in Emacs" works and opens the file.
What is wrong with the first approach ?
ok, I solved my issue. The problem comes from my misunderstanding of the difference between ScriptEditor and the Automator. If I use the Automator to create an App and use the former script instead of creating an App using the ScriptEditor, then it works as expected.
One can simplify the process by creating an App in Automator and running a shell script instead of wrapping the command in Ascript.

How to delete the preview icon of a PNG file with AppleScript or the Terminal?

I am trying to automatically delete the preview icon of a PNG file (or many files) that I have generated with Photoshop.
I know how to manually do this: I can select the files, hit command+shift+i, select the icon on that window and hit the delete key to delete the files; but I would prefer to do this automatically with an AppleScript (or a Terminal command that I will then embed in my AppleScript with a do shell command)... I have searched the web for days but I have found nothing that helps me.
So, does anyone know of an AppleScript or Terminal command that could be used to delete the preview icon of a PNG (or JPEG) file?
You can use the setIcon method from the NSWorkspace class to delete icon of the file. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/
A Cocoa-AppleScript Applet
use framework "AppKit"
use scripting additions
set myFiles to choose file with prompt "Select PNG files" with multiple selections allowed
set sharedWk to current application's NSWorkspace's sharedWorkspace()
repeat with tFile in myFiles
(sharedWk's setIcon:(missing value) forFile:(POSIX path of tFile) options:0)
end repeat
Or an AppleScript using a do shell script
set pyScript to quoted form of "from AppKit import NSWorkspace; import sys; NSWorkspace.sharedWorkspace().setIcon_forFile_options_(None, sys.argv[1].decode('utf-8'), 0)"
set myFiles to choose file with prompt "Select PNG files" with multiple selections allowed
repeat with tFile in myFiles
do shell script "/usr/bin/python -c " & pyScript & " " & (quoted form of POSIX path of tFile)
end repeat
Here's some links:
https://en.wikipedia.org/wiki/Cocoa_(API)
https://developer.apple.com/library/mac/releasenotes/ScriptingAutomation/RN-AppleScriptObjC/
https://en.wikipedia.org/wiki/PyObjC
JavaScript with Objective-C Bridge --> https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html

Automator Apple Script to convert jpeg image

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.

How can I launch an Automator application using AppleScript?

I am trying to use Remote Buddy to control Photo Booth, but I need a way to switch between Still and Video modes, my solution to this was to use an Automator app to select one or other of the two radio buttons when a remote button is pressed.
I've created the .app, and it works fine when I double click it from the desktop, but I need a way to launch the .app from within Remote Buddy, and AppleScript seems to be my only option.
tl;dr
I need to be able to launch an Automator .app file using AppleScript, but can't figure out the correct syntax.
If I made an Automator app named Untitled I would start it by using this command tell application "Untitled" to activate
Once you create an application using one of the following methods, that application can be accessed in any other script via its name. It's defined globally, as is any other application on your mac. Just use the tell application "app Name"
Two ways of creating an application :
activate app ((system attribute "HOME") & "/Desktop/test.app/")
You could also use the automator shell command.
automator test.workflow
automator test.app
automator test.workflow -v # verbose
automator -i lol test.workflow
echo lol | automator -i - test.workflow
automator -i $'lol\nlol2' test.workflow # \n separates input strings
automator -d somevar=somevalue test.workflow
You would first name your automator app for example "photobooth.app" then you would go in applescript an type in
tell application "photobooth.app"
activate
end tell
I do it directly with Automator scripting. This does not access the app, but rather the workflow. It is advantageous, because you can edit the settings/contents of some of the individual workflow items.
I suppose my answer would be better suited for the question:
How can I launch an Automator Workflows using AppleScript?
I find that saving the Automator action first avoids problems. e.g.
set theWorkflowName to "Merge PDF Files"
set myWorkflow to make new workflow with properties {name:theWorkflowName, path:POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)}
set myWorkflow to open POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)
Merge PDF Files Droplet
on open the_Droppings
-- CONVERT INPUT LIST OF ALIASES TO POSIX PATHS
repeat with itemStep from 1 to count of the_Droppings
set item itemStep of the_Droppings to POSIX path of item itemStep of the_Droppings
end repeat
tell application "Automator"
activate
set myWorkflow to open POSIX file "/Users/USERNAME/Dropbox/Scripts/Automator/Workflows/merge PDF files.workflow"
set actionsList to name of Automator action of myWorkflow
set firstAction to item 1 of actionsList
tell myWorkflow
(*
get index of Automator action firstAction
get input types of Automator action firstAction
get path of Automator action firstAction
get path of Automator action firstAction
get value of setting of Automator action firstAction
*)
set value of setting of Automator action firstAction to the_Droppings -- MUST BE LIST OF POSIX PATHS
end tell
end tell
end open

Is there a way to trigger Finder's "quick look" window with Applescript?

I am using Applescript to automate some tasks in the OSX Finder. The script opens up a folder and selects the first image in that folder. I would like it to also bring up the "quick look" window (exactly as if the user had pressed the space bar).
I did find a way to fire up quick look from the command line using qlmanage, but that brings up a static quick look window, which is no longer tied to the finder selection.
Code so far:
property folderPath : "/Volumes/Media/Images"
on run {}
tell application "Finder"
activate
set imageFolder to folder (folderPath as POSIX file)
set imageFile to first item of imageFolder
select imageFile
-- show quick look?
end tell
end run
If you don't want to do it by scripting the Finder you can run the following shell command
qlmanage -p thefile
In an Applescript you might do this like
do shell script "qlmanage -p " & "thepath/thefile"
Depending upon what you are doing this might be much easier. Especially if you primarily just have a set of paths.
If you have an existing Applescript path you can send it like this
set p to POSIX path of mypath
do shell script "qlmanage -pr " & quoted form of p
Updated (with thanks to Kevin Ballard):
tell application "System Events" to keystroke "y" using command down
Note: this requires that "enable access for assistive devices" is selected in the "Universal Access" control panel.

Resources