Applescript application and Open with - applescript

I have a simple script
on run
display dialog "Hello" buttons {"Ok"}
end run
I saved it as application (let's say myapp.app)
If I just launch it from finder by double clicking then it works (i.e. shows the dialog box), but I want to use it for opening files.
The problem:
For example I have file.xyz, then right-click it to "open with" and choose my.app. In this case my.app doesn't start (no icon at the dock panel, no dialog box shown).

You can use an open handler to drag items on the application like this:
on open of theFiles
-- Executed when files are dropped on the application
display dialog "There are " & (count of theFiles) & " files dropped" buttons {"OK"}
end open

Related

How can I create a .app file to open DMG files with, to run AppleScript with that file as input?

What I ultimately want to do is have a .app that I can open DMG files with. When the DMG file is opened the app, it opens the DMG in a new Finder tab and then shows the Toolbar in Finder after half a second. AppleScript for the later would look like this:
delay 0.5
tell application "Finder"
tell the front window to set toolbar visible to true
end tell
I tried looking up how to open a DMG file in a new Finder Tab with AppleScript but didn't find a solution. If I end up not finding one, then I'll see if I can do so with a Terminal command and then do it in AS with do shell script. For the file input, I tested this code based off what I found here but it doesn't work, it just creates a copy of the DMG:
on run {input, parameters}
if input is {} then
set inputFile1 to ¬
quoted form of POSIX path of ¬
(choose file with prompt "Please select a file to process:")
else
set inputFile1 to quoted form of ¬
(POSIX path of first item of input)
end if
display dialog inputFile1
end run
I did a bunch of googling as well for how to do so but I found nothing. What I can do to end up with an AppleScript that does everything I described?
I would open the dmg with hdiutil which seems to open the image into a standard window:
set runDMG to quoted form of POSIX path of (choose file)
do shell script "/usr/bin/hdiutil attach " & runDMG
Basically, you get the posix path of your dmg file and feed it to hdiutil attach. The disk image will open and —for me at least— consistently opens in a default finder window (i.e. same size as other windows, list view, sidebar visible, toolbar visible).
I should add that typically when I double-click on a dmg, it opens in a 'drag to applications folder' window which is icon view and shows only the app and the applications folder.
Separately, this code seems to work with bringing the disk image's folder to the front window.
tell application "Finder"
parent of startup disk
--> computer container of application "Finder"
open computer container
set target of window 1 to (last item of window 1 as alias)
--> disk "Key Codes" of application "Finder"
end tell
There are (at least on my Sierra mac), three items in the 'computer' folder: 'Network', 'Remote Disc', the startup volume. When you open a dmg, the virtual appears here as a 'removable'. I've found that when you get a list of folders in the computer container, the removable is the last item in the list. I don't know if that ever changes, and I haven't played around with multiple disk images simultaneously but it provides a starting point for specifying the desired window if for some reason, it doesn't open automatically.

Automator service in OSX High Sierra

Automator service at the active finder window. The idea is to create a folder template at the active finder window with choosing the new folder
dialog.
Service:
ask for folder name
make new folder
run shell script
The Automator service fails and I am not sure why. The path of the active finder window is not reaching the variable thePath which is assigned to the new folder Automator action.
The screen shots displays a simplified version of the service and the error.
Can I get idea of what is wrong?
Try inserting this code in a run AppleScript action, into your workflow.
set theName to text returned of (display dialog ¬
"INSERT A NEW FOLDER NAME" default answer ¬
"Enter Desired Name Of New Folder" hidden answer false ¬
buttons {"Cancel", "OK"} ¬
default button ¬
"OK" cancel button ¬
"Cancel" with title ¬
"CHOOSE A NAME" with icon 1 ¬
giving up after 30)
tell application "Finder"
set currentTarget to target of window 1 as alias
set the name of (make new folder at currentTarget) to theName
end tell

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

Customize look of window dialog in AppleScript?

i would like to know if it's possible to change the look of Dialog Window in Applescript, for example change the icon (note, stop, caution) by a custom picture or icon instead.
And if it is possible to change the typography (alignment, font, bold ... etc.)
If yes, could you help me with an example for my code or provided a link to a good tutorial please!
set folderName to text returned of (display dialog "Please enter new folder name:" default answer "Folder_Name")
set loc to choose folder "Choose Parent Folder Location"
try
tell application "Finder"
set newFolder to make new folder at loc with properties {name:folderName}
end tell
display dialog "Successfully! Want to reveal the new folder?" with icon note buttons {"Cancel", "Go to my new folder"} default button "Go to my new folder" cancel button "Cancel"
if button returned of the result = "Go to my new folder" then
tell application "Finder"
reveal newFolder
activate
end tell
end if
end try
You can't change the typography. However you can use:
display dialog "hi" with icon withIconFile
where withIconFile is an alias or file reference to a ‘.icns’ file
This is better for me !
display dialog "My custom icon " buttons {"Cancel", "Continue"} default button "Continue" with icon file "Path:to:my.icon.icns"
You can also do:
display dialog "Hello" buttons {"Cancel", "Continue"} with icon {"/Users/" & (do shell script "whoami") & "/path/to/picture.jpg"}
The do shell script part just makes it work, even if you send it to your friends, although I would then recommend downloading the image from the internet with curl and placing it in a folder. Sorry if it is a bit confusing

How to keep an application activated/in foreground while a script is running?

I'm using Applescript to automate deployment of applications from Xcode 4.
I use System Events to click the menu items in the menu bar for that. I have the whole thing working, but with one quirk. If the user clicks elsewhere while my script is running, that is the XCode 4 window goes out of foreground, my entire script fails. Is there a way to force Xcode to be in the foreground from my script?
Now, if only Xcode 4 was at least as scriptable as Xcode 3, I wouldn't have to resort to GUI automation.
You can just use the activate command for every click call to ensure that the application is in the foreground. It's not ideal. Really if you're going to use System Events for scripting input like this you have to just accept that the user can't really use the computer whilst the script is running!
If you can break the script down into parts that require user input and parts that don't, you could present a dialog to the user saying something like, "Are you ready to continue with the script? You'll have to leave your computer for a while!"
... and then when it's finished, "Feel free to use your computer again now!"
This might make the script a little less obtrusive. Just a suggestion.
you should put up some of loading image or something while it is running also anytime you call a gui you should run a loop with a timeout that activates the app then checks for the existence of the items you want to click then when it exists click it and get out of the loop
EDIT
helpful link
A combination of mcgrailm and James Bedford's answers worked.
I put the menu click for "Edit Schemes…" in a loop until the edit scheme sheet becomes existent.
I also had to activate the application just before I clicked "Run without building".
The code:
tell application id "com.apple.dt.Xcode"
activate
end tell
tell application "System Events"
tell process "Xcode"
repeat until sheet 1 of window 2 exists
click menu item "Edit Scheme…" of menu "Product" of menu bar item "Product" of menu bar 1
tell application "Xcode"
activate
beep
end tell
end repeat
tell sheet 1 of window 2
set destination to pop up button 2 of group 1
click destination
set target to "iPad 4.3 Simulator"
click menu item target of destination's menu 1
set buildConfig to pop up button 3 of group 2
click buildConfig
click menu item "Debug" of menu 1 of buildConfig
click button "OK"
end tell
tell application "Xcode"
activate
beep
end tell
tell application id "com.apple.dt.Xcode"
activate
end tell
click menu item "Run Without Building" of menu 1 of menu item "Perform Action" of menu "Product" of menu bar item "Product" of menu bar 1
end tell
end tell

Resources