motion-macos-screenshothelper 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d53c26fc23d123da23be8e88e87f93f6bad72326
4
+ data.tar.gz: 41afb45a500129d76e7ca0003716048063050742
5
+ SHA512:
6
+ metadata.gz: 3155828d0ceff7e31e78d8ecf02bbc8b944631d9a3658b1c655d57ca651fb591cf322daf55d7eb5a9f1862cfa902e158d83f8f87cc6f67c539359cc3979b397e
7
+ data.tar.gz: 82917f750b210a870cbc20a774a4bb19392dd3f73e0e802e46a20a7c226a8b9b07f364d0b7f3c2c5aa7ec711f09ff0b7f640e70b1537993a84730ee643de2c51
@@ -0,0 +1,56 @@
1
+ # motion-macos-screenshothelper
2
+
3
+ A gem for taking automated screenshots in macOS applications written in Rubymotion.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'motion-macos-screenshothelper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install motion-macos-screenshothelper
18
+
19
+ ## Usage
20
+
21
+ Add the ScreenshotHelper tool to your spec files like so:
22
+
23
+ ```
24
+ describe 'My application' do
25
+ before do
26
+ @app = NSApplication.sharedApplication
27
+ @sh = Motion::ScreenshotHelper.new(my_config)
28
+ end
29
+
30
+ it 'takes a screenshot' do
31
+ @sh.shoot.should == true
32
+ end
33
+
34
+ it 'takes a screenshot after waiting' do
35
+ # do something import with your GUI
36
+ @sh.pause(1.0) # time in seconds to wait
37
+ @sh.shoot.should == true
38
+ end
39
+ end
40
+ ````
41
+
42
+ You may set the following config options when calling ```Motion::ScreenshotHelper.new`:
43
+
44
+ ```
45
+ my_config = {
46
+ screenshot_mode: Motion::ScreenshotHelper::SCREENSHOT_MODE_FULLSCREEN, # for full-screenshots
47
+ shots_dir: 'screenshots', # the target dir for the screenshot files
48
+ file_basename: 'screenshot', # the basename for the screenshot files (a counter will be added) e.g. 'screenshot_0.png'
49
+ app_name: NSBundle.mainBundle.infoDictionary['CFBundleName'] # your application name needed for focusing the app with AppleScript
50
+ }
51
+ ```
52
+
53
+ ScreenshotHelper will make use of the screencapture command line tool.
54
+
55
+ ## Contact
56
+ https://vt-learn.de
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined?(Motion::Project::Config)
4
+ raise "This file must be required within a RubyMotion project Rakefile."
5
+ end
6
+
7
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
8
+ Motion::Project::App.setup do |app|
9
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
10
+ end
@@ -0,0 +1,100 @@
1
+ module Motion
2
+ # A class for taking screenshots in Ruby apps running on the Mac
3
+ class ScreenshotHelper
4
+
5
+ attr_accessor :config
6
+
7
+ SCREENSHOT_MODE_FULLSCREEN = 0
8
+ SCREENSHOT_MODE_MAINWINDOW = 1
9
+
10
+ def initialize(config)
11
+ self.config = default_config.merge(config)
12
+ @counter = 0
13
+ clear_dir
14
+ self
15
+ end
16
+
17
+ def default_config
18
+ {
19
+ screenshot_mode: SCREENSHOT_MODE_FULLSCREEN,
20
+ shots_dir: 'screenshots',
21
+ file_basename: 'screenshot',
22
+ app_name: NSBundle.mainBundle.infoDictionary['CFBundleName']
23
+ }
24
+ end
25
+
26
+ def target_filename
27
+ "#{config[:shots_dir]}/#{config[:file_basename].to_s}_#{@counter.to_s}.png"
28
+ end
29
+
30
+ def shoot
31
+ begin
32
+ focus_app
33
+ system screenshot_cmd
34
+ @counter += 1
35
+ rescue Exception => e
36
+ puts "Exception #{e}"
37
+ return false
38
+ end
39
+ true
40
+ end
41
+
42
+ def pause(sec=1.0)
43
+ intervall = NSDate.dateWithTimeIntervalSinceNow sec
44
+ NSThread.sleepUntilDate intervall
45
+ end
46
+
47
+ private
48
+
49
+ def check_shots_dir
50
+ self.config[:shots_dir] = File.absolute_path(config[:shots_dir])
51
+ fail(IOError, "#{config[:shots_dir]} is not a directory") unless File.directory?(ndir)
52
+ end
53
+
54
+ def app
55
+ NSApplication.sharedApplication
56
+ end
57
+
58
+ def screenshot_cmd
59
+ screen_rect = NSScreen.mainScreen.frame
60
+ case config[:screenshot_mode]
61
+ when ScreenshotHelper::SCREENSHOT_MODE_MAINWINDOW
62
+ fr = app_window.frame
63
+ x_origin = fr.origin[0]
64
+ y_origin = screen_rect.size[1] - fr.origin[1] - fr.size[1]
65
+ capture_frame = "#{x_origin},#{y_origin},#{fr.size[0]},#{fr.size[1]}"
66
+ rv = "screencapture -R#{capture_frame} #{target_filename}"
67
+ else
68
+ rv = "screencapture #{target_filename}"
69
+ end
70
+ rv
71
+ end
72
+
73
+ def app_window
74
+ return app.mainWindow unless app.mainWindow.nil?
75
+ return app.keyWindow unless app.keyWindow.nil?
76
+ app.windows.first
77
+ end
78
+
79
+ def focus_app
80
+ scpt = <<SCPT
81
+ tell application "#{config[:app_name]}"
82
+ activate
83
+ end tell
84
+ SCPT
85
+ osascript scpt
86
+ end
87
+
88
+ def osascript(script_src)
89
+ error = Pointer.new('@')
90
+ apple_script = NSAppleScript.alloc.initWithSource(script_src)
91
+ apple_script.executeAndReturnError(error)
92
+ puts "NSAppleScript::error #{error.value.inspect}" unless error.value.nil?
93
+ end
94
+
95
+ def clear_dir
96
+ cmd = "rm -rf #{(config[:shots_dir] + '/*').dump}"
97
+ system cmd
98
+ end
99
+ end
100
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-macos-screenshothelper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Kolb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A gem for taking automated screenshots in macOS applications written
28
+ in Rubymotion
29
+ email:
30
+ - admin@vt-learn.de
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - lib/motion-macos-screenshot_helper.rb
37
+ - lib/project/screenshot_helper.rb
38
+ homepage: https://vt-learn.de
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.5.1
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: This gem lets you take screenshots of your macOS app written in Rubymotion
62
+ test_files: []