Ported OSX Obj-C/CG Screenshot Code to js-ctypes -

2019-05-29 08:14发布

问题:

I ported some objective-c/CoreGraphics code that takes a screenshot of multiple monitors but it's not working. The jsctypes guys won't be able to help because the section is just me answering questions lol

The objc/cg code is from here: https://stackoverflow.com/a/28247749/1828637

My jsctypes type/struct/declares are here: https://github.com/Noitidart/NativeShot/blob/mac-troubleshooting/modules/ostypes_mac.jsm

The jsctypes port is here: https://github.com/Noitidart/NativeShot/blob/mac-troubleshooting/modules/workers/MainWorker.js#L443-656

And the last line is line 643 which causes the crash:

var rez_writeToFile = ostypes.API('objc_msgSend')(data, ostypes.HELPER.sel('writeTofile:atomically:'), myNSStrings.get(OS.Path.join(OS.Constants.Path.desktopDir, 'full_ss.png')), ostypes.CONST.YES);

This coincides with the objectivec of

[data writeToFile:@"/tmp/screenshot.png" atomically:YES];

except i changed path to ...desktop/...png

I wrote to sepearte gists, to align the lines in text diff software to make sure I did everything.

  • Here is objc/cg: https://gist.github.com/Noitidart/8affcd8bee60d22dcb52
  • and here is jsctypes: https://gist.github.com/Noitidart/3d4a4e8df625dbea8cc1

I dont think i missed anything i dont know why it crashes on that final line

Do any of the ObjC/CG people see any type definition that is wrong or something that I missed (for example the ObjC code uses compound statments I'm not sure I understood/translated them correctly in the port).

回答1:

Wowww so after struggling for like some hours then posting this I realize it right away haha

Casing typo in selector!! I had writeTofile:atomically instead of writeToFile:atomically!

If anyone would like to test this out, it's a simple firefox addon test case.

  1. Install an addon that allows installing addons from github repos from here: https://addons.mozilla.org/en-US/firefox/addon/github-extension-installer/
  2. Go to commit on this branch that works here: https://github.com/Noitidart/NativeShot/tree/f5f2e8606d3c5c657e901239d3ceb8c8c79a4494
  3. Then click the "Add to Firefox" button at bottom right, below the "Download Zip" button
  4. Click on the toolbar button icon that gets added to firefox, as seen in this screenshot: http://i.imgur.com/n7lSxje.png
  5. Check desktop you now have a single png file with a screenshot of all the monitors, huge props to @KenThomas!