I am able to show custom icon in AppleScript using below code
set iconPath to "/Users/dutt/myFolder/AppIcon.icns" as POSIX file
set theContent to " hi"
display dialog theContent with icon file iconPath with title "Hello" buttons {"Cancel", "Ok"} default button "Ok"
When I import foundation using below code, it is throwing error file doesnot contain icon
use framework "Foundation"
use scripting additions
set iconPath to "/Users/dutt/myFolder/AppIcon.icns" as POSIX file
set theContent to " hi"
display dialog theContent with icon file iconPath with title "Hello" buttons {"Cancel", "Ok"} default button "Ok"
I believe it is related to path issue, apple script is not able to get the icon path after using foundation framework
My suggestion is to use relative HFS paths, path to home folder points to the home folder of the current user.
This avoids the POSIX path - POSIX file - alias dance and works with and without Foundation
set iconPath to alias ((path to home folder as text) & "myFolder:AppIcon.icns")
set theContent to " hi"
display dialog theContent with icon iconPath with title "Hello" buttons {"Cancel", "Ok"} default button "Ok"
Note the missing file keyword after with icon
The issue is caused by the way file references are resolved once you start implementing Objective-C frameworks into your AppleScript.
The solution is to build your file references using coercions. So, change:
icon file iconPath
to:
icon (iconPath as alias)
Or it might even be necessary to change:
set iconPath to "/Users/dutt/myFolder/AppIcon.icns" as POSIX file
to:
set iconPath to "/Users/dutt/myFolder/AppIcon.icns"
then build the file reference in your display dialog command like this:
icon (iconPath as POSIX file as alias)
Related
on macOS in Finder there is the option "Open with >" in the context menu of files, which shows a menu with all available apps being able to open the file. So the user can choose, with which app the file must be opened.
I would like to open this menu from code for a specific file.
In Windows it goes like this
Process.Start("rundll32.exe", "shell32.dll, OpenAs_RunDLL " + file);
but I can't figure out, how this goes in macOS.
Maybe you can adjust this following AppleScript code to suit your needs.
If you have a file selected in Finder, and you run this following code in Script Editor.app, it will open a dialog for you to choose a new application to open your selected Finder file with, then will open that file with your chosen app.
activate
set chosenApp to (choose application with prompt ¬
"Choose Your Preferred Default Application" as alias)
tell application "Finder"
set thisFile to selection -- The Selected File In Front Finder Window
set appID to id of chosenApp
try
open thisFile as text using application file id appID
on error errMsg number errNum
display dialog "Please Go Back And Select A File In Finder To Be Opened." & linefeed & ¬
"Then Come Back And Run This Code Again" buttons {"Cancel", "OK"} default button "OK"
end try
end tell
Steps:
Start the terminal by pressing command-space --> type in: terminal
when the terminal is running you switch to the applications folder:
'cd Applications'
You can run the apps like this: open -a GIMP-2.10.app
more info:
https://osxdaily.com/2007/02/01/how-to-launch-gui-applications-from-the-terminal/
I'd like to have Preview open a pdf and save it to another folder, or save it with a different name to the same folder (not sure which method is needed yet).
I can open the file fine from a specific folder location.
I tried invoking the Save As command, but I don't want the user to have to perform this step, I want to do it automatically to avoid user data entry errors.
Here are the steps that work:
set the save_location to "/FolderOne/FolderTwo/FolderThree/"
set the file_name to "Past Due.pdf"
tell application "Preview"
open file "FolderOne:FolderTwo:Past Due.pdf"
tell application "Preview" to activate
tell application "System Events" to keystroke "S" using {option down, shift down, command down}
end tell
This following AppleScript code will display a dialog for you to choose your original PDF file to be duplicated, display a dialog for you to name your duplicated file, and display a dialog for you to choose a location to save your duplicated file. It will then open your duplicated file in Preview.app, leaving your original un-opened.
This AppleScript code works for me using the latest version of macOS Mojave.
property originalLocation : missing value
property originalFileName : missing value
property originalFileNameExtension : missing value
property moveToLocation : missing value
property newFileName : missing value
activate
set originalFile to (choose file with prompt ¬
"Choose Which Document To Duplicate & Open In Preview.app")
tell application "System Events"
set fileInfo to originalFile's properties
set {originalFileName, originalFileNameExtension, originalLocation} to ¬
{name of fileInfo, name extension of fileInfo, (path of fileInfo) as alias}
end tell
set theOffset to offset of originalFileNameExtension in originalFileName
set shortName to text 1 thru (theOffset - 2) of originalFileName
activate
set newFileName to text returned of (display dialog ¬
"Choose A Name For Your Duplicate File" default answer ¬
(shortName & " Copy") as text buttons {"Cancel", "OK"} ¬
default button 2 cancel button 1 ¬
with title "Choose A Name For Your File" with icon 1 ¬
giving up after 120) & "." & originalFileNameExtension
activate
set moveToLocation to (choose folder with prompt ¬
("Choose A Save To Location For " & newFileName) ¬
default location originalLocation) as text
do shell script "cp " & quoted form of POSIX path of originalFile & ¬
" " & quoted form of POSIX path of ((moveToLocation & newFileName) as text)
do shell script "open -b com.apple.Preview " & ¬
quoted form of POSIX path of ((moveToLocation & newFileName) as text)
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
I want to write a script using Automator that opens a folder in a particular location, after receiving user input. Doesn't have to be Applescript.
So the steps would be:
Dialog asking for Folder name
Verifying the folder exists
If exists, open folder in new finder window
if not exist, display message
Any help would be greatly appreciated.
instead of asking user to type folder name into a dialog box, why not use the standard "choose folder" which provide usual file/folder selection graphic interface ? on top of that, it will also make sure the folder selected exists !
Also it is possible to user instruction "if My_Folder exists then ..." to check if folder (or file) exists
Example of direct user selection : 5 first lines are asking folder selection in folder Documents and detect user cancellation. Next lines are just example to display result
try
set SelectFolder to choose folder with prompt "choose folder" default location "/Users/My_User/Documents"
on error number -128
set SelectFolder to ""
end try
if SelectFolder is "" then
display alert "user did not select a folder"
else
display alert "User selection is " & SelectFolder
end if
The following script does exactly what you're asking for.
on run
set thisFolder to (choose folder with prompt "Choose a folder...")
if folderExists(thisFolder) then
-- display dialog "The folder exists." buttons {"OK"} default button 1
tell application "Finder" to open thisFolder
else
display dialog "The folder has been deleted" buttons {"OK"} default button 1
end if
end run
on folderExists(theFolder)
tell application "Finder"
try
set thisFolder to the POSIX path of theFolder
set thisFolder to (POSIX file thisFolder) as text
--set thisFolder to theFolder as alias
return true
on error err
return false
end try
end tell
end folderExists
That said, note that a folder that has been selected using AppleScript's Choose Folder command must always exist. It can't be selected if it doesn't exist. Therefore, my first thought is that you don't need this, but if you need to check whether a folder exist for a different reason, then the folderExists function will do the job.
You could use the following script to do what you're asking for (no choose folder, this script uses a dialog like you asked for):
set folderChosen to text returned of (display dialog "Enter the path to the folder you want to open:" default answer "")
try
do shell script "ls " & folderChosen
do shell script "open " & folderChosen
on error
display alert "Folder Doesn't Exist" message "The folder path you entered doesn't exist. Make sure to enter a path, e.x. /Users/USERNAME/MyFolder." as critical
end try
This AppleScript uses do shell script "ls " & folderChosen to verify if the folder exists, and then opens the folder if ls successfully runs, indicating the folder path exists. Otherwise, it displays an alert with a warning sign explaining the path they entered doesn't exist.
If you want this to run in automator, you could use Automator's Run AppleScript feature.
I hope this solution helps you solve your problem!
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.