How do I get this apple script to pop up a dialog? - applescript

The following apple script times out. How do I get it to work?
This runs from inside a bash script and it's supposed to pop up a dialog. It works fine on one mac. But it times out on my computer. However, it start to work after I run the script once inside Apple's Script Editor. Also, if I run the Script Editor version before the script timeout, the dialog pops up! Is this a permissions issue? What is going on and how do I fix it?
Bash Script:
osascript <<EOT
tell app "System Events"
text returned of (display dialog "Enter value:" default answer "default" buttons {"OK"} default button 1 with title "Type A Value")
end tell
EOT
Error:
46:159: execution error: System Events got an error: AppleEvent timed out. (-1712)

The display dialog command is not a System Events command. It is a Standard Additions command, therefore there is no need for a tell block
Also, inserting an activate command before the display dialog command will ensure the dialog window will appear on top of any currently opened programs or windows.
Try this...
osascript <<EOT
activate
text returned of (display dialog "Enter value:" default answer "default" buttons {"OK"} default button 1 with title "Type A Value")
EOT

Related

macOS: How to open the "Open with" menue from code?

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/

Bash Script Calling AppleScript Error

I have 2 scripts 1st is bash and 2nd is AppleScript on my macOS 10.14 mojave. I am calling AppleScript from Bash and getting error. If I run AppleScript directly it will run without any issue.
If I run bash script to execute AppleScript I will get dialog "ERROR: Unable to find application identifier."
If I run AppleScript in Script Editor it will show correct (org.mozilla.firefox) application identifier with dialog.
Why its not showing application identifier when I run bash script to execute AppleScript.
Bash script :
#!/bin/bash
/usr/bin/osascript /Users/mymac/Desktop/tst.scpt
AppleScript :
property IDI : "NOTSET"
try
set IDI to bundle identifier of (info for (path to application "Firefox"))
display dialog " Identifier is " with title "Test" buttons {"Exit"} default answer IDI
end try
if IDI is "NOTSET" then
display dialog "ERROR: Unable to find application identifier." with title "Test" buttons {"Exit"}
return
end if
I cant put both script into one need them separately in scripts.

Running shell commands in ITerm2 without tab closing after completion

Probably a minor syntax issue that I'm getting wrong, but can't find the solution in the ITerm2 documentation. I'd like to create an applescript that opens an ITerm window with three tabs, each running various shell commands (ls, cd, echo, etc.) with the tab the remaining open after those commands have run. The opening tabs part is working fine, but it appears that as soon as the commands run, the tab closes (if I don't provide any commands, the tab will remain open.) For my script here:
tell application "iTerm2"
create window with default profile
tell current window
create tab with default profile command "echo abc"
create tab with default profile
end tell
end tell
Instead of "echo abc" what should I put there so the echo command will run in the tab, but leave me with a cursor for me to type in more commands instead of the tab immediately closing thereafter?
Instead of using the create tab ... command, use a separate write text command. For example, this is a script I use to open a terminal to a specific directory:
tell application "iTerm"
create window with default profile
tell current session of current window
write text "cd " & directory & "; clear"
end tell
end tell
Using the "write text" suggested by whereswalden I settled on the following, works well:
tell application "iTerm2"
create window with default profile
tell current window
tell current session
write text "echo abc"
end tell
create tab with default profile
tell current session
write text "ls -la"
end tell
create tab with default profile
tell current session
write text "cd mydir"
end tell
end tell
end tell

AppleScript cannot remove files from /Library

I write an AppleScript:
to check if two specific dictionaries (File1 and File2) files exit in /Library/Dictionaries/
if either of the two files exits, then completely delete that file and the other
The script works fine in AppleScript Editor. I then save the script as an app, test it, and the app also works just fine. I mean both the script in AppleScript Editor and the saved app detect and remove both File1 and File2 from /Library/Dictionaries/.
However in PackageMaker Post Action, when called, the app removes neither File1 nor File2 although it detects them and even shows the dialog message (see code line below).
Here is the code:
tell application "System Events"
if exists (application process "Dictionary") then
tell application "Dictionary" to quit
end if
end tell
try
set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"
tell application "Finder"
if exists file theOtherFile1 then
display dialog "File1/File2 exits. Do you want to remove it?" with title "Note" buttons {"No", "Yes"} default button "Yes"
if the button returned of the result is "No" then
delay 2
else
do shell script "rm -R /Library/Dictionaries/File1.dictionary" with administrator privileges
do shell script "rm -R /Library/Dictionaries/File2.dictionary" with administrator privileges
end if
end if
end tell
end try
delay 5
tell application "Dictionary" to activate
Try this:
tell application "System Events"
if exists (application process "Dictionary") then
tell application "Dictionary" to quit
end if
end tell
try
set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"
tell application "Terminal" to activate
tell application "System Events"
keystroke ("sudo chmod 777 " & theOtherFile1) as string
display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
keystroke return
keystroke ("chmod 777 " & theOtherFile2) as string
display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
end tell
do shell script "rm -rf /Library/Dictionaries/File1.dictionary"
do shell script "rm -rf /Library/Dictionaries/File2.dictionary"
This should do the trick.
I'm using a PC at the moment, otherwise I'd check first, but I think this will run correctly.

Close Terminal window from within shell script (Unix)?

Is there a way to close a Terminal window from within a shell script? I have a .command file that should just get out of the way once it's done.
Using exit 0 will cleanly terminate the script.
Whether Terminal window stays open is user-configurable. The default is to always stay open. To change this:
Terminal.app > Preferences > Profiles > Shell
- "When the shell exists:"
> Close if the shell exited cleanly
- "Ask before closing:"
(•) Never
-- OR --
(•) Only if there are....
When "Close if shell exited cleanly" is used, the script will close the window if the exit result is 0, which is the default if nothing went wrong.
Since you don't want to delete all Terminal windows, first change the name of your window from "Terminal" to something else:
echo -n -e "\033]0;My Window Name\007"
Then at the end of the script, use:
osascript -e 'tell application "Terminal" to close (every window whose name contains "My Window Name")' &
You can use apple script to quit the terminal app. Add the following to your script -
osascript -e 'tell application "Terminal" to quit'
This will give you a popup confirming to close the app. You can disable this in Terminal preferences.
Alternatively, you can also use killall command to quit the app. The following would work just as well.
killall Terminal
Note:
Just as a side note, you can freely add the above commands to your script and it would work as you want. However, there are few caveats. First being you will limit the ability of your script to work on different boxes. Secondly, it would be safer to use nohup so that any commands that are currently running won't quit due to quitting of the Terminal app.
This works for me:
#!/bin/sh
{your script here}
osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit
This will work for closing just your windows opened with a .command file but leave things already running in other terminal windows. I know that I almost always have either sass or grunt watch something so I don't want to quit terminal totally.
closeWindow() {
/usr/bin/osascript << _OSACLOSE_
tell application "Terminal"
close (every window whose name contains "YourScriptName")
end tell
delay 0.3
tell application "System Events" to click UI element "Close" of sheet 1 of window 1 of application process "Terminal"
_OSACLOSE_
}
This will close the Terminal window for your script and keep any other Terminal windows open as long as their window titles don't match. For it to work Terminal will have to be added to the list of applications permitted to use the Accessibility framework. You can also cycle through Terminal windows with a repeat command and close every window x that contains a UI element "Close" on sheet 1.
I find the best solution for this is to use Automator to create a true OSX application which will work the same way regardless of how your system is configured. You can have the Automator run your shell script, or you can embed the shell script itself in Automator.
Here is how you do it:
Run Automator (in Applications).
Choose "New Document" and when it
asks "Choose a type for your document" choose "Application"
In the
left panel, select "Utilities" then "Run Shell Script".
Type in your
script commands in the workflow item in the right panel. You can either call another
shell script, or just put your commands in their directly.
Save the
Application, which will be a full-fledged Mac App. You can even
cut-and-paste icons from other apps to give your script some
personality.
#!/bin/bash -x
{your script here}
. exit 0
kill -9 $PPID
you can also create a shortcut for your script:
cp yourscript.sh ~/bin/yourshortcutnamewhateveryouwant
then type
yourshortcutnamewhateveryouwant
will run whatever is writen into script at any directory.

Resources