danielpclark / clock_window

Clock your own desktop activity! Open for feature requests and contributors!
MIT License
6 stars 2 forks source link

Mac support #1

Closed danielpclark closed 8 years ago

danielpclark commented 8 years ago

Need some one with a Mac OS to implement and verify.

  1. https://gist.github.com/gerad/961627
  2. https://porkrind.org/missives/calling-applescript-from-ruby/
  3. http://stackoverflow.com/questions/5292204/macosx-get-foremost-window-title
karimmtarek commented 8 years ago

Hey @danielpclark I can pick this up, what needs to be done exactly?

danielpclark commented 8 years ago

We need to test and prove what works for getting the active window name in thew Mac OS X (the 3rd link most likely or the 1st link otherwise).

Then we need to change ClockWindow::OScommand in clock_window.rb#L18-L21 to detect the environment operating system.

@os = RbConfig::CONFIG['host_os']

And the case in ClockWindow::OScommand#active_window can be changed to

case @os
when /linux/i

for Linux and you can add a when clause for the Mac OS to get the string of what you need to execute and a proc of how to format the output. The expected output of the OScommand#active_window method is

["executable string", ->input{ input.format_the_string_result_from_executable_string }]

If we need to provide the script for getting the active window name in the Mac OS ourselves then we should create an executable script file in the /exe directory.

Let me know if you have any more questions.

karimmtarek commented 8 years ago

I'll give it a go and feedback :) thanks a lot for the details 👍

karimmtarek commented 8 years ago

@danielpclark can you please explain to me what those lines are doing

exe = "xprop -id $(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2) _NET_WM_NAME"
format = ->str{ str.match(/.*\"(.*)\"\n\z/)[1][0..60] }
[exe, format]

and this

begin
  loop do
    x = ClockWindow::ClockIt.new.active_window
    @hash[x] = @hash[x].to_f + 0.25
    sleep 15
  end
ensure
  @hash = {"*---------- WINDOW NAME ----------*" => "minutes"}.merge(@hash)
  File.open("timelog.json","w") do |f|
    f.write(JSON.neat_generate(@hash,aligned:true,around_colon:1))
  end
end
danielpclark commented 8 years ago

exe is a string that will executed on the terminal.

xprop and cut are both Linux commands and the Linux command will return something like.

`xprop -id $(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2) _NET_WM_NAME`
# => "_NET_WM_NAME(UTF8_STRING) = \"user@computer: ~\"\n"

But we only want the last bit of the string which happens to be the name of the active window (the terminal I'm working in). So we need to format it. So the input string and output string for fromat looks like this.

format = ->str{ str.match(/.*\"(.*)\"\n\z/)[1][0..60] }
format.call("_NET_WM_NAME(UTF8_STRING) = \"user@computer: ~\"\n")
# => "user@computer: ~"

And now we've successfully executed the command to get the window name and formatted the string to get it. So we write our OS command returns to give both the executable string and any formatting required. If no formatting is required you can do ->val{ val }

The second part you asked about you don't need to change anything. If you change OScommand to work with the Mac the Ruby script will work as is. But the basics of it is every quarter of a minute get the window name from ClockWindow::ClockIt.new.active_window and add the quarter of a minute to the hash by window name. The ensure part at the end is for when the user presses CTRL-C it will save the hash to a file name timelog.json.

karimmtarek commented 8 years ago

@danielpclark this already taken care of?

danielpclark commented 8 years ago

@karimmtarek Mostly yes. @omegahm grabbed it. Although I believe there's one more thing to be done in the ocascript https://github.com/danielpclark/clock_window/issues/9 . It's simple but it's less Ruby oriented.