Friday, December 6, 2019

#1 2018-12-24 05:53:29 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 616
Website

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 :

1)

do shell script "
/usr/bin/python <<'END'
import sys
import time
from Quartz.CoreGraphics import *

def scroll_up():
    event = CGEventCreateScrollWheelEvent(None, kCGScrollEventUnitLine, 1, -1)
    CGEventPost(kCGHIDEventTap, event)

scroll_up()
END"

 
2)

def RelativeMouseScroll(x_movement, y_movement):  #Movements should be no larger than +- 10
    scrollWheelEvent = CGEventCreateScrollWheelEvent(
            None,  #No source
            kCGScrollEventUnitPixel,  #We are using pixel units
            2,  #Number of wheels(dimensions)
            y_movement,
            x_movement)
    CGEventPost(kCGHIDEventTap, scrollWheelEvent)

Offline

 

#2 2018-12-25 01:37:02 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 109

Re: Scrolling with CoreGraphics

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.

Offline

 

#3 2018-12-25 01:01:09 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 616
Website

Re: Scrolling with CoreGraphics

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?

Offline

 

#4 2018-12-26 04:50:40 am

technomorph
Member
Registered: 2017-12-14
Posts: 125

Re: Scrolling with CoreGraphics

Check appkit ans postinf an event

https://developer.apple.com/documentati … guage=objc

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

Offline

 

#5 2018-12-27 09:55:18 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 109

Re: Scrolling with CoreGraphics

Joy wrote:

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

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:

Applescript:

ObjC.import('CoreGraphics');
nil=$();

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

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

Applescript:

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

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

scrollY(-1)

These act/respond instantaneously.

Offline

 

#6 2018-12-28 03:52:21 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 616
Website

Re: Scrolling with CoreGraphics

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 sad
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

Last edited by Joy (2018-12-28 04:06:00 pm)

Offline

 

#7 2018-12-29 04:41:50 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 109

Re: Scrolling with CoreGraphics

I ought to have stated that my pertinent system info is

AppleScript version: 2.7
System version:      10.13.6

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.

Offline

 

#8 2019-01-01 02:15:38 am

technomorph
Member
Registered: 2017-12-14
Posts: 125

Re: Scrolling with CoreGraphics

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

https://macscripter.net/viewtopic.php?id=46580

Offline

 

#9 2019-01-01 12:22:25 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 616
Website

Re: Scrolling with CoreGraphics

I created a script library and put that (Java)script into: home/library/Script Libraries/
Result:
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 neutral

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:

MbGM0SK.jpg

Last edited by Joy (2019-01-01 02:13:01 pm)

Offline

 

#10 2019-01-08 03:19:19 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 616
Website

Re: Scrolling with CoreGraphics

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? 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

Last edited by Joy (2019-01-08 03:22:18 am)

Offline

 

#11 Today 04:07:05 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 616
Website

Re: Scrolling with CoreGraphics

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:

Applescript:

"call ("&myapp&")

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)