Applescript - Copy Image, Paste as Icon - macos

For organizational purposes, via a hotkey, I'd like to be able to quickly copy a selected image file (as is in Finder), then paste it as the icon for a selected folder, without having to go into 'Get Info' for the folder.
Is this possible?

You probably need two scripts: one to copy image (not a file but image itself), other to paste image as icon.
Copy image:
tell application "Finder"
open selection using (path to application "Preview") -- open with Preview
end tell
tell application "Preview"
tell application "System Events"
keystroke "a" using command down -- select all
keystroke "c" using command down -- copy
keystroke "w" using command down -- close window
end tell
end tell
Paste image as icon:
tell application "Finder"
activate
tell application "System Events"
keystroke "i" using command down -- open info
-- set focus on icon image
tell process "Finder"
set img to (image 1 of scroll area 1 of front window)
set focused of img to true
end tell
keystroke "v" using command down -- insert image
keystroke "w" using command down -- close window
end tell
end tell
Than you can bind these two scripts to some hotkeys. I use FastScripts for that purpose.
Note 1: you may need to enable access for assistive devices under the SystemPreferences -> Accessibility
Note 2: if some parts of the scripts are not work (for example Preview opened, but image not selected, etc.) you should try to play with delays.

Related

AppleScript - How to set the target/path of a finder window prompt when it's prompted from an App (not from the Finder)?

I am using AppleScript to automate a process on a GUI.
I use the script to click a button "Add Source Folder" (similar to opening a file in any apps) in the GUI, which prompts an integrated Finder window (image here).
In practice, once the prompted finder window opened, I'd like the path to be set automatically from an alias object theFolderToProcess's (which would've been set previously in the script).
I am unable to set its path because I have nowhere to set the alias theFolderToProcess within the in-app prompt finder window. So how do I get the script to navigate to the path of the alias?
Here is the code:
set theFolderToProcess to choose folder with prompt "Step 1: Select the folder to convert:"
tell application "System Events"
tell application process "MyApp"
tell its window "MyApp"
activate
click button "Add Source Folder"
set uiElems to UI elements
end tell
end tell
end tell
With UI Elements, I get sheet 1 of window "MyApp" of application process "MyApp" of application "System Events" where sheet 1 is the prompt window.
Note: Setting the path in the prompt window doesn't work.
From within the 'open' dialogue, you should be able to use the Finder's Go to Folder… function, or command-shift-G (or whatever it is on your OS). Then use keystroke to enter your desired location. The location reference should follow the format below rather than using your alias. Follow up with a key code to accept your location. I find the short delays help with keyboard scripting but edit as desired.
set posixFolder to posix path of theFolderToProcess
--> "~/Desktop/exports/"
delay 0.1
key code 5 using {command down, shift down} -- Go to folder…
delay 0.1
keystroke posixFolder -- folder must exist
--> keystroke "~/Desktop/exports/" -- folder must exist
delay 0.1
key code 76 -- type Return (Go button)
There is a full example here but this should be sufficient.
A don't know your "App", so following example is using TextEdit. If you successfully open OPEN dialog using other way (click button "Add Source Folder"...) then replace corresponding code line of my script with yours. My script shows how to set automated delays as well. And how to click the "Go" button at the end.
set processName to "TexEdit"
set sourceFolder to POSIX path of (path to pictures folder)
tell application processName to activate -- bring to front
tell application "System Events"
-- this is instead of your 'click button "Add Source Folder"'
-- it opens OPEN dialog window
keystroke "o" using command down
-- wait
repeat until window "Open" of process processName exists
delay 0.02
end repeat
-- open go to path sheet
keystroke "g" using {command down, shift down}
repeat until sheet 1 of window "Open" of process processName exists
delay 0.02
end repeat
-- keystroke full posix path
keystroke sourceFolder
-- go
click button "Go" of sheet 1 of window "Open" of process processName
end tell

Applescript - won't work when used as a droplet

I'm trying to create a script to use as a droplet where I drop a file onto it, automator opens it in Adobe Acrobat, then applescript runs an action inside of Acrobat to save it as an optimized PDF into a specific folder, then opens that new file in preview and uses a quartz filter to reduce the size of the PDF.
If I have a file already open in Acrobat and just run the script, it executes the action within Acrobat, saves the file, then opens preview (haven't gotten any further). But if I drop the file onto the droplet it just gives me an error message.
Here's the code:
on run {input, parameters}
tell application "System Events"
tell application process "Acrobat"
tell application "Adobe Acrobat Pro" to activate
-- runs the custom action in Acrobat
click the menu item "SAVE in 0-SEND" of menu 1 of menu item "Action Wizard" of the menu "File" of menu bar 1
end tell
end tell
tell application "System Events"
keystroke return
-- clears the action finished message
end tell
tell application "Adobe Acrobat Pro"
activate
close documents saving no
quit
end tell
-- this doesn't work yet
tell application "Finder"
tell application "Preview"
open input
end tell
end tell
end run
I would recommend something along these lines...
on run
set aFile to choose file with prompt "Please select the file to process"
processPDF(aFile)
-- cleanup, quit the application
tell application "Adobe Acrobat Pro" to quit
end run
on open (theFiles)
repeat with aFile in theFiles
processPDF(aFile)
end repeat
-- cleanup, quit the application
tell application "Adobe Acrobat Pro" to quit
end open
on processPDF(theFile)
-- open your file
-- do something with theFile here.
-- close the file
end processPDF

With Applescript I need to use System Events to navigate a Finder Window that opens within an application

I'm currently writing some Applescript to import some material into software which doesn't have much applescript support and virtually no documentation on Applescript. The two methods are to somehow adapt this script to work:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
import media into document 1 from these_items
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to
However when I try to use "import media into document 1 from ________" I always get an error, in every incarnation of that combination.
My second approach then is using System Events to navigate through the menu bar to import everything. When I get to the import part of things where I can navigate and select the files, a finder window pops up. My question is:
How do I navigate this finder window within the application? This is like the inception of scripts. A finder window within a program.
I tried a few simple things like calling the front window of finder to navigate somewhere and what not. The current script that gets me to the import window is:
tell application "IsadoraCore"
activate
delay 2
tell application "System Events"
click menu bar item "File" of menu bar 1 of application process "IsadoraCore"
click menu item "Import Media..." of menu "File" of menu bar item "File" of menu bar 1 of application process "IsadoraCore"
end tell
end tell
Any help would be appreciated!
I would guess the "import media" window is a Finder window where you select a file to import. This window is easy to handle. To select the file you must have the posix path of the file. In that window if you keystroke "shift-command-g" then you get a text field where you can keystroke the posix path. For example, let's say I want to open a file in TextEdit. The file is on my desktop with the name test.txt. I can do this...
set filePath to (path to desktop as text) & "test.txt"
set posixPath to POSIX path of filePath
set aShortDelay to 0.5
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "o" using command down -- bring up the "open dialog window"
end tell
delay aShortDelay
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "g" using {shift down, command down} -- bring up the "goto folder sheet" in the open dialog window
delay aShortDelay
keystroke posixPath -- enter the posix file path
delay aShortDelay
keystroke return -- dismiss the "goto folder sheet"
delay aShortDelay
keystroke return -- dismiss the "open dialog window"
end tell
Notice I have delays after every command. That is good practice when you're using this type of scripting. You don't want the code to run faster than the computer can perform the tasks so the delay gives the computer time to keep up with the code. Notice that I use a variable for the delay. This way I can play with aShortDelay making it longer or shorter as needed to make the script run properly.
Good luck.
Aren’t you asking Isadora to import a list of folders? Perhaps it can only handle one folder at a time?
I don’t have Isadora to test, but you might try this:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
repeat with the_item in these_items
import media into document 1 from the_item
end repeat
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to

AppleScript Clicking On dialog box

In PCSX, (ps1 emulator), i'm trying to automate the steps to play an iso. So, i'm doing this:
set thepath to path to me
set thesecondpath to POSIX path of thepath
set thethirdpath to "Contents/PSX/ROMS/img.bin"
set thefourthpath to "/Contents/PSX/PCSX.app"
set thefifthpath to thesecondpath & thefourthpath
set theultimatepath to thesecondpath & thethirdpath
tell application thefifthpath
activate
tell application "System Events"
keystroke "i" using {command down}
keystroke theultimatepath
delay 1.0
tell process "PCSX"
click button "Go"
end tell
key code 53
end tell
end tell
Running from the AppleScript Editor won't work. I made it to work running from the App it creates. PCSX and the img.bin are inside the Generated Package.
after pressing command+i, it opens a "Go to the folder" dialog, and i need to click Go and then Open
But doing this way, it won't find the dialog box. What am i doing wrong?
If Go and Open are the default buttons, try:
tell application "System Events"
keystroke return
delay 2
keystroke return
end tell
Although I don't have PCX installed here is an example of how to click the Go button from Finder's Go to Folder command.
tell application "System Events"
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
end tell
The reason your script won’t work from AppleScript Editor is that the “me” in “path to me” is the application that ran the AppleScript. When you are running the AppleScript in AppleScript Editor, that means AppleScript Editor itself. When you saved your AppleScript as a script application and ran it, the path to me pointed to your script application, because it was running its own AppleScript.
Also, this is incorrect:
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
The “Go” button is not on the window “Go to Folder.” It’s on a sheet which is attached to a Finder window which has the name of whatever folder is currently being viewed. So you have to describe the button as being on sheet 1 of window 1:
tell application "System Events"
tell process "Finder"
click button "Go" of sheet 1 of window 1
end tell
end tell
… but keep in mind that in another app, a similar looking button on a sheet may be on sheet 1 of group 1 of group 2 of window 3. UI Scripting is complicated.

Applescript file dialog with UI scripting

I am trying to open a file in a not so scriptable area of an application.
I got halfway there by using UI scripting to select the proper menu item, but this opens a standard file dialog.
How can I set the destination of the file dialog with Applescript?
ahh ok this should get you going on the right path
tell application "Safari"
activate
-- do menu select gui script first
set posixpath to "/path/to/a/folder/that/exists"
tell window 1
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke posixpath
delay 1
keystroke return
end tell
end tell
end tell

Resources