I'm learning AppleScript and my first program is a Hello World(of course!):
display dialog "Hello World"
But when I try to run this I got the error:
The result of a numeric operation was too large.
Why this?
How I can solve it?
See: The Ultimate Beginner’s Guide To AppleScript – Mac.AppStorm
tell application "Finder"
display dialog "Hello World"
end tell
Related
I am trying to make an applescript to be able to cmd+L to get ctrl+L to clear lines in iTerm as suggested by this answer.
After copying a working, example applescript from here shown below,
tell application "System Events" to keystroke "l" using control down
I am trying to change the application to "iTerm2" as shown below,
tell application "iTerm2" to keystroke "l" using control down
so the shortcut in not global, but I get a Syntax error:
A identifier can’t go after this “"”.
Removing the quotes (this also works in the working example) brings up a different Syntax error:
"A identifier can’t go after this identifier."
What am I doing wrong?
Thanks to #gurkensaas's comment for the correct answer. I post the complete working script below for other people.
tell application "iTerm" to activate
delay 0.1
tell application "System Events"
try
keystroke "l" using control down
end try
end tell
Edit: I ended up using Better Touch Tool for this shortcut since it was much more responsive and fast.
Hello I'm trying to build a very simple Automator service to automatically turn on Auto Loop within a Keynote document. I'm not a very familiar with AppleScript, but I have used it a few times to do minor things. I'm gettin the following error when using this script within an automator service:
Synatax Error
"Can’t set «class aulp» to true."
Im sure it is something very obvious, but I'm not that well versed in AppleScript. Any help would be greatly appreciated.
Here is the script
on run {input}
set thisDocument to input
tell application "Keynote"
tell thisDocument
set auto loop to true
save thisDocument
end tell
end tell
end run
I am still learning the basics of AppleScript.
When I try to run a simple "Hello World" script like:
tell application "TextEdit"
activate
end tell
tell application "System Events"
keystroke "Hello World!"
key code 36
end tell
The first time I run it, it writes "Hello World!" in TextEdit as it should.
But the second time, it writes "Hello World!" in Script Editor.
From then on, it will only write in Script Editor.
Am I missing something obvious in the script?
Or is there something about OS X that I should be looking at?
Thanks in advance! Feel free to respond with long winded answers, Kbase articles, or other discussion threads that I may have missed.
tell application "TextEdit" to activate
tell application "System Events"
tell application process "TextEdit" to set frontmost to true
keystroke "Hello World!" & return
end tell
Try this group of code. I'll explain how it works below, using comments (--).
tell application "TextEdit"
-- Activate TextEdit and bring it to the foreground
activate
-- Create a new document and assign it to a variable called 'myDocument'
set myDocument to make new document
-- Activate the above mentioned new document
tell myDocument
-- Add text to the above mentioned new document
set its text to its text & "Hello, world!"
end tell
end tell
tell application "Finder" to say "this is a test"
tell aplication"finder"
activate
repeat 5 times
make new Finder window
end repeat
end tell
I am just learning different coding and know quite a bit about html, css, and javascript. I am completely new to apple script editor.
Since you're new to applescript I'll give you a basic tip to learn. Only tell an application to do something that it knows how to do. Each application knows how to do specific things and applescript knows how to do things by itself too.
I tell you this because the "say" command is an applescript command, not a Finder command. So there's no reason to tell the Finder to say anything. As you get more complex in your scripts you will find errors if you tell the wrong application to do something. As such you can run the say command by itself. Try this and it will work by itself...
say "this is a test"
The easiest way to know what each application understands is to look in the dictionaries. In Script Editor, under the file menu choose "open dictionary". You can choose any application but for this example open the Finder dictionary. You can search through it to find what the Finder knows how to do. You'll notice it doesn't have the "say" command thus you know not to tell the Finder to use the say command. You can type "say" into the search field and you'll see it doesn't return any results.
If you open the dictionary for "StandardAdditions" you'll find say in there. That's additional things applescript knows by itself.
Good luck.
The part tell aplication"finder" has two typos and is missing a space. It should be tell application "Finder"
The whole think should look like this which builds for me.
tell application "Finder" to say "this is a test"
tell application "Finder"
activate
repeat 5 times
make new Finder window
end repeat
end tell
I am trying this simple GUI script to open a new window of Safari:
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
try
tell menu bar 1
tell menu bar item 3
click menu item 1
end tell
end tell
on error theError
display dialog ("An error occurred while performing requested action" & theError) buttons "OK" default button "OK"
end try
end tell
end tell
but it is giving this error message:
Expected end of line but found """
Can anyone suggest me where I may be wrong?
Thanks,
Miraaj
Wow, that was weird. Your script broke AppleScript Editor. After running your script and it not working... I tried to recompile the script and then the error you posted starting showing up. So somehow your code caused AppleScript editor to break and thus the error. I had to quit and relaunch AppleScript Editor to get it working again.
I used the application UI Browser and found the problem. Your reference to the menu item was wrong. There's an extra menu in there that we can't see... and you didn't reference that extra menu. This is the problem with gui scripting. And even if a gui script works it may break at some future date as an application is updated. As such avoid gui scripting if at all possible.
Anyway, here's what your code should look like...
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari"
try
tell menu bar 1
tell menu bar item 3
click menu item 1 of menu 1
end tell
end tell
on error theError
display dialog ("An error occurred while performing requested action " & theError) buttons "OK" default button "OK"
end try
end tell
end tell
EDIT:
As I mentioned in my comment below, if you can't find a native command from an application's dictionary, the next most reliable method is using keyboard shortcuts. Most menu items have them. For example, if I wanted to open a new tab in a window that menu item has the keyboard shortcut command-t. So we can use that like this. Note there is a native command to open a new tab without using keystrokes, I'm just showing this as an example.
tell application "Safari" to activate
tell application "System Events"
keystroke "t" using command down
end tell
end
Keyboard commands don't usually change between application updates whereas gui commands often do because programmers redesign their interface in updates... and when that happens gui scripting goes haywire. One of the gotcha's with both gui scripting and keystrokes is that sometimes the script goes too fast and these techniques can't keep up with the speed of the program, so they often error. When this happens you need to slow down the script using small delays to allow the interface to keep up with the script.