I have a file (/test.txt) that contains iCal event info.
Friday, May 6, 2011 4:00:00 PM
05/08/2011 11:20:00 PM
summary
location
Friday, May 6, 2011 4:00:00 PM
05/08/2011 11:20:00 PM
summary
location
And this is the applescript to read this file to make iCal event.
set Names to paragraphs of (read ("/test.txt"))
set my_list to {}
set temp_list to {}
repeat with nextLine in Names
if length of nextLine is greater than 0 then
set temp_list to temp_list & nextLine
else
copy temp_list to end of my_list
set temp_list to {}
end if
end repeat
repeat with e in my_list
set my_list to {}
tell application "iCal"
tell calendar "Todo"
set new_event to make new event at end of events
tell new_event
repeat with j from 1 to count e
set content to item j of e
if j is 1 then
set start date to date content --> Error
end if
if j is 2 then
set end date to date content
end if
if j is 3 then
set summary to content
end if
if j is 4 then
set location to content
end if
end repeat
end tell
end tell
end tell
end repeat
Running this code gives me an error
Why is this error?
you had two problems first you need to add the last event after you loop then you were trying to set the date inside the ical tell block which doesn't work for some reason so I took it out of the tell block i also improved the code a bit
set theData to read ("path:to:test.txt" as alias)
set ParaCount to count of paragraphs of theData
set my_list to {}
set temp_list to {}
repeat with i from 1 to ParaCount
set thispara to paragraph i of theData
if thispara is equal to "" then
copy temp_list to end of my_list
set temp_list to {}
else
set temp_list to temp_list & thispara
end if
end repeat
copy temp_list to end of my_list -- copy the last one to the list
repeat with aEvent in my_list
set {start_date, end_date, sum, loc} to aEvent
set start_date to date start_date
set end_date to date end_date
--reduced to single line
tell application "iCal" to make new event with properties {start date:start_date, end date:end_date, summary:sum, location:loc} at end of events of calendar "Todo"
end repeat
end repeat
Related
Feeling like a real bone-head here, just trying to figure how how I can get this script to insert the name from the selection in artist and put it into Album artists.
tell application "iTunes"
set theTracks to (item 1 of (get selection))
set theTracks to selection
repeat with theTrack in theTracks
set albumartist to artist of theTrack
end repeat
end tell
Like this:
tell application "iTunes"
set theTracks to (item 1 of (get selection))
set theTracks to selection
repeat with theTrack in theTracks
set albumartist to artist of theTrack
set album artist of theTrack to albumartist -- the missing line
end repeat
end tell
In real life I would probably skip the intermediate variable and write the pair of lines as one line, like this:
set album artist of theTrack to (get artist of theTrack)
Or even better:
tell theTrack to set album artist to (get artist)
I want to add 100 names in the list. I'm using Calabash, so I have the .feature file:
When I fill the item field with "3"
And press the button Adicionar
And I repeat the previous 2 steps 100 times
My .rb file:
...
When(/^I repeat the previous (\d+) steps (\d+) times$/) do |steps, times|
How can I implement this .rb file? The last thing I tried, I got the error:
Undefined dynamic step: "2" (Cucumber::UndefinedDynamicStep)
Below is a quick hello world example for google (I don't have your code, so can't properly do an example for your site).
The When is what we are interested in really.
Given(/^I navigate to "([^"]*)"$/) do |url|
$driver.navigate.to url
end
When(/^I search for "([^"]*)"$/) do |search_term|
# Loop through 100 times
100.times do
# Clear any text present
$driver.find_element({css: 'input[name="q"]'}).clear
# Type in the request
$driver.find_element({css: 'input[name="q"]'}).send_keys(search_term)
# Fire the request with the return key
$driver.find_element({css: 'input[name="q"]'}).send_keys(:return)
# Give time for the process to complete before a reset (This could also go first)
sleep 1
end
end
Then(/^I (?:should|must) see some results$/) do
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { $driver.find_element({css: 'h3.r'}) }
end
Doing a for loop, like in the When above, could also have a maximum set by a new captured integer:
When(/^I search for "([^"]*)" (\d+) times?$/) do |search_term, times|
# Loop through an amount of times
times.to_i.times do
$driver.find_element({css: 'input[name="q"]'}).clear
$driver.find_element({css: 'input[name="q"]'}).send_keys(search_term)
$driver.find_element({css: 'input[name="q"]'}).send_keys(:return)
sleep 1
end
end
This would mean that you don't have to dynamically set up capturing of previous steps (which is doable, I'm sure, but would be a giant amount of effort for what you seem to be wanting to do here).
You have a list of names then you can pass it using the data table and you can write your a combined step as following:
Given I add following names in the list:
|Ram|
|Abdul|
|Scot|
and then you can write the step definitions using nested steps as following in .rb file:
Given(/^I add following names in the list:$/) do |table|
data = table.raw
data.each do |row|
steps %{
When I fill the item field with "#{row[0]}"
And press the button Adicionar
}
end
end
When(/^I fill the item field with "([^"]*)"$/) do |arg1|
pending # Write code to enter the the item
end
When(/^press the button Adicionar$/) do
pending # Write code to click the Adicionar button
end
If you simply want to fill the names with same name "3" then you can write your combined step as following:
Given I fill the item field with "3" 100 times
And then you can write the steps definition as following:
Given(/^I fill the item field with "([^"]*)" (\d+) times$/) do |name, loop_count|
loop_count.to_i.times do
steps %{
When I fill the item field with "#{name}"
And press the button Adicionar
}
end
end
When(/^I fill the item field with "([^"]*)"$/) do |arg1|
pending # Write code to enter the the item
end
When(/^press the button Adicionar$/) do
pending # Write code to click the Adicionar button
end
Using Ruby Tk, I'm wanting to create sets of radio buttons in nested loops: an outer loop for devices (Foo, Bar, Baz), and inner loop for actions on each device (Start, Stop, Pause, Nuke).
Current code:
require 'tk'
device_names = %w/Foo Bar Baz/
action_names = %w/Start Pause Stop Nuke/
button_variables = Array.new(device_names.size)
root = TkRoot.new
foo = bar = nil
device_names.each_with_index do |device_name, i|
TkLabel.new(root) do
text device_name
pack {}
end
action_names.each_with_index do |action_name, j|
TkRadiobutton.new(root) do
text action_name
value j
variable button_variables[i]
pack {}
end
end
end
Tk.mainloop
This produces the 12 radiobuttons, but when I click on any of the 4 Pause buttons, for example, all 4 Pause buttons are set.
Other variations of the code produce the 12 radiobuttons, but all as one set; i.e., only one of the 12 is set at any given time.
What's needed to group these into 3 sets of 4 radiobuttons?
(This code is pared down from a much larger application, which is why it does not make much sense symantically.)
The value passed as variable is the name of a global variable.
The outer loop needs to dynamically define an instance variable, via instance_variable_set:
This code works:
require 'tk'
device_names = %w/Foo Bar Baz/
action_names = %w/Start Pause Stop Nuke/
root = TkRoot.new
device_names.each do |device_name|
TkLabel.new(root) do
text device_name
pack {}
end
instance_variable_set("##{device_name}", nil)
action_names.each do |action_name|
TkRadiobutton.new(root) do
text action_name
value action_name
variable device_name
pack {}
end
end
end
Tk.mainloop
I'm having trouble with this code (again).
I'm trying to get Ruby to check if tree is equal to any of the items in $do_not_scan and all I'm getting is a "cannot convert Array into String" error. Any way to fix such a thing?
My code:
#filesniffer by Touka, ©2015
$filecount = 0
$trees = Dir.entries("C:\\")
$do_not_scan = ["bootmgr", "BOOTNXT", "USER", ".", "Documents and Settings", "PerfLogs", "System Recovery", "System Volume Information", "$RECYCLE.BIN"]
def scan
$trees.each do |tree|
unless tree.include?($do_not_scan[0...8])
puts "scanning #{tree}"
entries = Dir.entries("c:\\#{tree}")
entries.each do |filename|
if filename == "**\*.dll"
puts "Found #{filename} in #{tree}"
end
end
end
end
end
def scan_loop
$trees.each do |tree|
scan
unless tree.include?($do_not_scan[0...8])
subtrees = Dir.entries("#{tree}")
subtrees.each do |tree|
scan
scan_loop
end
end
end
end
scan_loop
sleep
It looks like the following have to change in the scan and scan_loop methods:
$do_not_scan[0...8].include?(tree)
In place off:
tree.include?($do_not_scan[0...8])
This would be done in Ruby..I have provided what I have attempted thus far.
I am curious as to if it is possible to iterate over an excel workbook (so it would be multiple sheets) and basically index/record where everything is located. Lets say I have a workbook of 10 sheets. I want it to grab the first sheet, record that sheets name, then move to the first cell and begin indexing(not sure if correct word) the data on that sheet. It would record the cell location so for the first (1,A) and the data thats in it. I am trying to output the data into a format as such like a CSV file or something:
Some code I have written that basically just iterates over every sheet and every cell in a workbook (removes whitespaces) and grabs its data and puts into a CSV...no sheet names or cell numbers present. I am using the roo and csv gems:
require 'rubygems'
require 'roo'
#Classes Used
class ArrayIterator
def initialize(array)
#array = array
#index = 0
end
def has_next?
#index < #array.length
end
def item
#array[#index]
end
def next_item
value = #array[#index]
#index += 1
value
end
end
#Open up files to compare
w1 = Excelx.new ( "C:/Ruby/myworkbook.xlsx" )
$values = Array.new
i = 0.to_i
# Continue until no worksheets left
num_sheets = w1.sheets().size
while (i < num_sheets)
puts "i is currently : #{i}"
puts "length of sheet array is : #{num_sheets}"
#Grab first sheet of each workbook
w1.default_sheet = w1.sheets[i]
1.upto(w1.last_row) do | row |
1.upto(w1.last_column) do | column |
string = w1.cell(row, column).to_s
if (string.strip.empty?)
puts "Whitespace!"
else
$values << string
end
end
end
i = i + 1.to_i
end
count = 0.to_i
CSV.open('C:/Ruby/results.csv', "w") do |csv|
csv << ['String']
i = ArrayIterator.new($values)
while i.has_next?
csv << [i.next_item]
count += 1
end
end
I took the liberty to shorten your script while adding check on empty sheets which produced errors.
require 'roo'
w1 = Excelx.new ( "C:/Ruby193/test/roo/book1.xlsx" )
CSV.open("book1.csv", "w") do |csv|
w1.sheets.each do |sheet|
w1.default_sheet = sheet
if w1.first_row && w1.first_column
eval(w1.to_s).each do |index, value|
csv << [sheet, index, value]
end
end
end
end
which gives in book1.csv
Sheet1,"[1, 1]",a1
Sheet1,"[1, 2]",b1
Sheet1,"[2, 1]",a2
Sheet1,"[2, 2]",b2
Sheet2,"[1, 1]",aa1
Sheet2,"[1, 2]",bb1
Sheet2,"[2, 1]",aa2
Sheet2,"[2, 2]",bb2