Applescript copy name of selected image in preview - applescript

I have several images opened in one Preview window. I'm trying to copy the file name of the selected image in the window.
Currently, I'm using
tell application "Preview" to activate
tell application "System Events" to keystroke "c" using command down
This does not work, probably because it copies the file and its path (I only need the file name as string)
thanks a lot

Maybe this is along the lines of what you are looking for?
tell application "Preview"
set thePath to path of document of front window as POSIX file as alias
end tell
tell application "Finder"
set theName to name of (get properties of thePath)
end tell
set the clipboard to theName

set the text item delimiters to "/"
get the path of the front document in application "Preview"
--> "/Users/CK/Pictures/Screenshots/Screen Shot 2018-03-29 at 16.06.jpg"
get the last text item of the result
--> "Screen Shot 2018-03-29 at 16.06.jpg"
--OR--
get the path of the front document in application "Preview"
tell application "System Events" to get the name of the file result
--> "Screen Shot 2018-03-29 at 16.06.jpg"
--THEN--
set the clipboard to the result

Related

Applescript: how to click on checkbox only if is not already selected?

I want to enable option "Open in Low Resolution" for all ".app" files in the system.
This is my Applescript code:
on run argv
tell application "Finder"
set myFile to (POSIX file (item 1 of argv)) as alias
open information window of myFile
activate
tell application "System Events"
try
click checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
end try
end tell
close information window of myFile
end tell
end run
i run it with:
sudo find / -name *.app -type d -exec osascript myScript.scpt "{}" \;
This work very good: it looks for all ".app" files, pass the file to the script, the script will open the "information window" of the file, clicks the "Open in Low Resolution" checkbox and finally it closes the window.
Problem: i need to click on the Checkbox ONLY if is not already selected.
I tried with this solution (and other similar):
Tick a checkbox only if it's not selected
Nothing happens. No errors.
System Events, Applescripts ecc... are allowed to run on Security And Privacy Settings
This does not works:
on run argv
tell application "Finder"
--set myFile to (POSIX file (item 1 of argv)) as alias
set myFile to (POSIX file ("/Applications/App Store.app")) as alias
open information window of myFile
activate
end tell
tell application "System Events" to tell process "Finder"
try
set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
tell theCheckbox
set checkboxStatus to value of theCheckbox as boolean
if checkboxStatus is false then click theCheckbox
end tell
end try
end tell
tell application "Finder"
delay 1
close information window of myFile
end tell
end run
It simply does nothing.
The following sample (with an app Info Window opened):
tell application "System Events" to tell process "Finder"
set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info")
tell theCheckbox
if not (its value as boolean) then click theCheckbox
end tell
end tell
Will produce the following error:
Can’t make «class valL» of {«class chbx» "Open in Low Resolution" of
«class scra» 1 of window "App Store.app Info" of «class pcap» "Finder"
of application "System Events"} into type boolean
What can i do next?
Best regards.
I suspect the problem is that you've put everything inside a Finder tell block. The Finder is notoriously finicky about scripting; best to use it as little as possible.
Rearranging things to use System Events seems to fix the problem (though admittedly my old MacBook Pro doesn't have a retina screen, so I had to test this using a different checkbox). Notice that you don't need to activate the Finder or the information window; you can do this all in the background.
on run argv
set myAppPath to item 1 of argv -- should be a POSIX path
tell application "Finder"
open information window of file (POSIX file myAppPath as text)
end tell
tell application "System Events"
set appName to displayed name of disk item myAppPath
tell process "Finder"
tell window (appName & " info")
tell first scroll area
if (value of checkbox "Open in Low Resolution" as boolean) is false then
click checkbox "Open in Low Resolution"
end if
end tell
click (first button whose role description is "close button")
end tell
end tell
end tell
end run

Dynamic Slideshow using Applescript

I have a folder of images that gets updated from a camera taking pictures periodically throughout the day. I'm trying to write some applescript that will create a slideshow from a folder of images but also update as more are added without having to rerun the script. I started out trying to do quick look but couldn't get that working. Any ideas on how best to tackle this?
UPDATE
this is what I have hacked together so far:
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
I don't believe this works if I add photos behind the scenes though.
This is what I came up with and it works perfectly for what I need.
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
repeat while true
delay 60
tell application "System Events"
tell process "Finder"
keystroke "a" using command down
end tell
end tell
end repeat
A few caveats... since it is using quick look, you can't really do anything on the computer while this is running since it can't have any other app activate while it is running (quick look closes when this happens). Also the repeat section is required to get quick look to pickup the new additions to the directory without losing focus. Pretty nasty stuff, but I couldn't really find another easy way to do it!

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

Applescript - Copy Image, Paste as Icon

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.

Open URL in new Safari tab with AppleScript

Is it possible to use AppleScript to open a link in a new tab in Safari?
This will work:
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
I think this also does what you asked for, but it is much shorter and is less browser-specific:
do shell script "open http://www.webpagehere.com"
This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:
do shell script "open -a Safari 'http://www.webpagehere.com'"
This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):
tell application "Safari"
open location "http://stackoverflow.com"
activate
end tell
It opens a new window if "Open pages in tabs instead of windows" is set to never though.
tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:
set u to "http://ja.wikipedia.org/wiki/漢字"
tell application "System Events" to open location u
--tell application "Safari" to open location u
--do shell script "open " & quoted form of u
This opens a new tab even when new pages are set to open in windows:
tell application "Safari"
activate
reopen
tell (window 1 where (its document is not missing value))
if name of its document is not "Untitled" then set current tab to (make new tab)
set index to 1
end tell
set URL of document 1 to "http://stackoverflow.com"
end tell
tell application "System Events" to tell process "Safari"
perform action "AXRaise" of window 1
end tell
set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.
I've been using the following script to open hundreds of docs into tabs in a single window.
tell application "Safari"
tell window 1
make new tab with properties {URL:"http://foo.com/bar"}
make new tab with properties {URL:"http://foo.com/baz"}
end tell
end tell
But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:
tell application "Safari"
tell window 1
set URL of (make new tab) to "http://foo.com/bar"
set make new tab to "http://foo.com/baz"
end tell
end tell
Code:
tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell
One problem is that this only works if the system's language is set to English.
It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:theURL}
on error
open location theURL
end try
end tell
I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
Arrgh -- the one line is overflowing. Here it is without the code tags:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
I ended up using automator to do this which was much easier and it works.
You can try following approach:
//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
tell window 1
//create new tab and open specified URL
tab with properties {URL:"https://url.com"})
//make tab active
set visible to true
end tell
end tell
Also u can combine usage of Apple script within FastScript (free for 10 shortcut)
To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.
If you want to open new Window than new tab u can play within next:
tell application "System Events"
tell process "Safari"
click menu item "New window" of menu "File" of menu bar 1
end tell
end tell
Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.
Not the shortest solution but also works, and not only in English ...
tell application "Safari"
activate
end tell
tell application "System Events"
set frontmost of process "Safari" to true
keystroke "t" using {command down}
end tell
set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL
Worked for me in Safari v.11
tell application "Safari"
tell window 1
make new tab with properties {URL:"https://twitter.com"}
end tell
end tell
I found a way to open a new tab in the background with Safari.
tell application "Safari"
set the URL of (make new tab in window 1) to "your.url.net"
end tell
During the time I wrote this answer I made this
tell application "Safari"
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
end try
end tell
New version
tell application "Safari"
repeat
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
if theURL is "" then exit repeat
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"
if button returned of result is "No" then exit repeat
end try
end repeat
end tell
Any suggestions will be appreciate
Best regards

Resources