Keystroke and Integers

Having a problem with Keystroke and integers. Below is an example that I tried in TextEdit, but it doesn’t work. I can’t get the keystroke to do integers in any application. I am using Mavericks 10.9.2 if that might have something to do with it.

Any help would be appreciated.
Thanks, PolishPrince


set x to 0
activate application "TextEdit"
tell application "System Events"
	tell process "TextEdit"
		repeat 5 times
			delay 0.3
			set MyNum to x as text
			keystroke "Number "
			keystroke MyNum as text
			keystroke return
			set x to x + 1
			--display dialog MyNum
		end repeat
	end tell
end tell

What are you assuming to get ?

Here the script do exactly what it is coded to achieve, it type :

Number 0
Number 1
Number 2
Number 3
Number 4

The instruction
keystroke MyNum as text
may be edited as
keystroke MyNum
because MyNum is already defined as a text object.

As I am lazy I would not write so many instructions and would use :

set x to 0
activate application "TextEdit"
tell application "System Events"
	tell process "TextEdit"
		repeat 5 times
			delay 0.3
			keystroke "Number " & x & return
			set x to x + 1
			--display dialog MyNum
		end repeat
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) mardi 4 mars 2014 20:54:23

Thanks Yvan Koenig

Yes, I just tried it on another machine and it does work. Is there some odd reason that it wouldn’t work on my other machine. Both machines are running Mavericks 10.9.2

PolishPrince

Two tracks :

(1) wrong permissions
run Disk Utility to repair permissions on your startup volume.
If it works, some permissions were wrong.

(2) corrupted preferences file.
quit the application
copy its preferences file ( ~/Library/Containers/com.apple.TextEdit/Data/Library/Preferences/com.apple.TextEdit.plist )
to the Desktop and delete the original ( a password will be required )
open a test document in TextEdit
execute the script.
If it works, the preferences file was corrupted.
The protocol creates a new preferences file, you will have to redefine some preferences.

Let us know if these suggestions solve the problem.

Yvan KOENIG (VALLAURIS, France) mardi 4 mars 2014 22:47:04

I did both of the suggestions and they didn’t make any difference.
PolishPrince

May you try to work in an other account on the machine where the script fails ?
If it works we will know that the culprit is in the normal user account.
If it doesn’t, the culprit is in the set of files shared by every users.

Yvan KOENIG (VALLAURIS, France) mercredi 5 mars 2014 15:37:34

Yvan Koenig

Thank you so much for your interest in helping me.

I created a new account on my machine and it has no problem running the script. It appears to be an Applescript problem because I noticed it doesn’t work on other scripts that save Mail to PDF when the PDF name adds a number to it.
(Example “MyFile - 2.pdf”) . It will save the file as “MyFile - .pdf” leaving a space where the number would be.

PolishPrince

As the problem is specific to your account, the culprit is a component available in this one.

The standard location of AppleScript components is in the global part of the system, not in the account one.

May you check if there is a non-standard item in your account ?
It may be an application stored in ~/Applications
a prefpane stored in ~/Library/PreferencesPanes
an addition stored in ~/Library/ScriptingAdditions
a component stored in ~/Library/StartupItems

You may also boot in Safe mode:
http://support.apple.com/kb/HT1564?viewlocale=en_US&locale=en_US

This mode don’t use extraneous components AND clean a lot of caches files.

Yvan KOENIG (VALLAURIS, France) jeudi 6 mars 2014 10:32:39

A (very) long shot, but it may also be worth checking the keyboard layout used in the problem account, since ‘keystroke’ only recognises characters from the current “Input Source”. (System Preferences → Keyboard → Input Sources.)

Hello Nigel

If the keyboard layout doesn’t allow to type numerical values, how did the asker had written his script which contain some of them ?

Yvan KOENIG (VALLAURIS, France) jeudi 6 mars 2014 12:53:25

Hello.

We know it pertains to the account. At every step I’d start with killing system events in the process info window of Activity Monitor, so I know it hasn’t been left hanging by some other process. I’d also kill off any other process I have running that uses System Events. At this stage I’d just run my script, if it works now, then I know that some other process was the culprit. Next up is checking for any clashes with things in the ScriptLibraries folder (move it out of the way) and retry if it was there. If it stil doesn’t work then I’d move anything out of the way from the Scripting Additions folder, log out and log back in and retry. If it still doesn’t work, then I’d run system diagnostics, and find som other way to clean the System and Hardware Caches if Onyx still doesn’t work, then rebooting before trying again. If it doesn’t work at this point, then I’m at a loss.

It’s theoretically possible. :slight_smile: If you check “Automatically switch to a document’s input source” in the Input Sources tab of the Keyboard preferences, changing the input source only affects the frontmost open document. So as you bring different documents to the front, the active input source changes to the one last set for each document (while they’re open). So it’s possible to write and run a script in AppleScript Editor which activates another application ” bringing one of that application’s documents to the front and changing the input source ” and then ‘keystrokes’ characters which are incompatible with the new input source.

But I only offered the suggestion because the symptoms of the problem are tantalizingly similar to what happens when ‘keystroke’ is used with unavailable characters. It’s unlikely that PolishPrince would be using an input source with no digit characters and a space-like character mapped to key 0 without being fully aware of the fact and realising the connection.

Hello McUsrII

What you described is what is achieved when we apply Safe Boot.
http://support.apple.com/kb/HT1564?view . cale=en_US
Here, as the script behaves well in an other user account, the culprit is not in the main part of the System, it is in the current user account.

Yvan KOENIG (VALLAURIS, France) jeudi 6 mars 2014 15:56:32

Thanks Nigel for the explanations.

Trying to “guess” what is playing the fool in a machine which is not in front of me is really uncomfortable.

Sometimes it’s uncomfortable when the machine is in front of me but it’s an other story.

Yvan KOENIG (VALLAURIS, France) jeudi 6 mars 2014 16:10:47

The first steps I suggest, which is to restart system events and retry, remove any user procesess using system events and retry, remove any scripting additions, real ones and script libraries, and retry. Is all happening in the user account. And maybe, there is some trouble regarding this user account concerning the hardware caches. And if the user has something on his user account , that are in conflict, then this can corrupt hardware caches.

What I am basically saying is that you’lll need to remove one thing at a time that doesn’t come preinstalled that can make such a conflict until you find it.

It once helped me to stop corruption of the hardware caches to remove the stuff I saw created dylib conflicts in the system log.

Thanks Guys for all the interest to help me.

I haven’t done a complete reinstall on my machine for some time so probably the best thing for me to do is format a new drive and start all over. I tried my backup and it works the same way. If I do find what is causing this problem I will post it.
Thanks to all
PolishPrince

May you try to apply a safe boot once to see if it changes something ?

Yvan KOENIG (VALLAURIS, France) jeudi 6 mars 2014 21:53:44

Yvan Koenig

I did a safe boot and nothing changed. Something I thought was odd was while viewing my user folder while booted with another drive, my desktop icon (icon view) was displayed as an applescript icon. I booted back to my troubled drive and selected “get info” for the desktop and deleted the icon. The icon is correct now but that didn’t help my problem.

polishPrince

To all who tried to help

I finally found the problem of my script not printing integers. I noticed that my numbers keypad wasn’t working on the right side of my keyboard so I went into system preferences - Accessibility - Mouse & Trackpad. For some reason the checkbox “Enable Mouse Keys” was enabled. After unchecking that checkbox my scripts are working fine on the integers issue.

Thanks for your help and suggestions
PolishPrince

Thanks

Good to know what was wrong. It may be useful an other time.

Yvan KOENIG (VALLAURIS, France) vendredi 7 mars 2014 09:25:51