Scrolling with CoreGraphics

I found 2 code projects but both fail, maybe because they’re outdated. Anyway I cannot figure out how to make this code work for me. Please check both codes :

I tested script number 1) and it works, but it took on my system 6 seconds (!) for the event to execute a single scroll action.

I get errors with both handlers. (line 6 syntax error non - ASCII character xc2 - no encoding declared)

CK, did you add the first 6 lines + the last line to the 2nd code snipped?

Check appkit ans postinf an event

You’ll find events for scroll and parameters
You can set

Yes, to test code snippet 2), I substituted the function scroll_up for RelativeMouseScroll in code snippet 1), and kept the rest the same.

Both snippets appear to work, but have the long 6-second delay I mentioned. You might consider using JavaScript for Automation instead:


var event = $.CGEventCreateScrollWheelEvent(nil,$.kCGScrollEventUnitLine,1,-1);
$.CGEventPost($.kCGHIDEventTap, event);

Or, if you want to call it from an existing AppleScript script, then:

to scrollY(dx)
	local dx
	run script "ObjC.import('CoreGraphics');

	var event = $.CGEventCreateScrollWheelEvent(nil,$.kCGScrollEventUnitLine,1," & dx & ");
	$.CGEventPost($.kCGHIDEventTap, event);
	$.CFRelease(event)" in "JavaScript"
end scrollY


These act/respond instantaneously.

Wow CK this sounds simply awesome, and is exactly what I was looking for!

Just sounds, because on my machine (10.11) this Javascript causes the crash of any application able to run this handler :frowning:
Unfortunately I’ve no idea how to deal with Javascript. Script editor returns no errors but simply crashes. My installed JavaScript version is v. 1.1

I ought to have stated that my pertinent system info is[format]AppleScript version: 2.7
System version: 10.13.6[/format]As for why yours crashes, I couldn’t say without looking at your system logs at the time of the crash. It might be that your version of AppleScript (and JXA) is too early to handle the C-type data references, but that’s a total guess.

AppleScriptObjC isn’t able to handle C-type data objects so there isn’t an AppleScript equivalent of the JavaScript code.

But your Python functions work, albeit in a delayed fashion. However, what I do notice is that, when run as native Python code from the terminal, the functions respond and act instanteously. Therefore, it’s the instantiation of a shell process via AppleScript by way of do shell script that is, unsurprisingly, causing the delay to occur.

Therefore, I’d recommend using these functions from within an Python script and, if you need to employ AppleScript functionality, then call AppleScript from Python instead of calling Python from AppleScript.

have a peek a Shane’s code to be able to access your java code saved as a script library.

I created a script library and put that (Java)script into: home/library/Script Libraries/
The same, this JavaScript crashes Script editor or any application calling that library. I suspect the code works (actually it does something) but is not compatible with my Os.
A shame :expressionless:

My Os : 10.11.0
AppleScript: 2.5
JavaScript: 1.1

Of course I’d like a solution which acts immediately, else the whole concept gets pointless. Pity that python can’t be loaded quicker. On my Os python doesn’t work at all:

Mostly I don’t need a scrolling feature… by the other hand, who knows where I’d implement that feature if I had it available ?
The solution seemed so close today

Since years I bother myself with this damn scrolling feature which is somehow ridiculous. The point is this need comes up all times. Why do I have to scroll manually for all the PDFs I read?
Auto scrolling is such a common feature on tablets and phones. Preview got just a generic AS support recently, I guess we should be happy about? :stuck_out_tongue:

Sometimes I wished to rewrite script dictionaries by myself… Even if this sounds egoistic, but this is knowledge far from my experience.
If there are other viable options usable for my Osx, 10.11 then please let me know. (Probably not, as people are very kind to share) anyway I had to put it down. Thanks

I wanted to reopen this question, the Javascript works on my newer machine

The Javascript preforms pretty fast, which is cool, but :
It executed sometimes yes, sometimes no - somehow odd. Plus, I’m unable to target a specific application where the script should scroll.

I tried to add after the first paragraph:

"call ("&myapp&") 

but here the script doesn’t recognize this command, and I thought" call" is part of the standard commands in Javascript ??