Get the variables in another script file - applescript

I use the "Run Script" command to run a program for another file. But it did not show the results. The result is a variable, how do I get this variable?
Code 1:
set xxx to {"ni", "bu", "en"}
set xxx2 to {"hao", "bu", "ni", "bu", "hao"}
repeat with item_number in xxx2
set booleanlist to {}
repeat with item_number_2 in xxx
if contents of item_number_2 is not contents of item_number then
set end of booleanlist to true
end if
if contents of item_number_2 is contents of item_number then
set end of booleanlist to false
end if
end repeat
set booleanlist_number to 0
repeat with booleanlist_number_2 in booleanlist
if contents of booleanlist_number_2 is true then
set booleanlist_number to booleanlist_number + 1
end if
if contents of booleanlist_number_2 is false then
exit repeat
end if
end repeat
if booleanlist_number = (count item of xxx) then
set end of xxx to contents of item_number
end if
end repeat
Code 2:
set xx to run script file "Macintosh HD:Users:mingxianzhao:Library:Mobile Documents:com~apple~ScriptEditor2:Documents:示例:run script and on run:Untitled.scpt" with parameters Character_used_for_the_query
choose from list xx

Add return xxx to the end of your Code 1 script.

As #foo already stated, you need to add the lines return xxx to end of the script "Code 1".
Having looked at "Code 1", it appears as if you are trying to combine the lists xxx and xxx2 into single list, but have the combined list contain only one copy of each item, i.e. you want a set of unique items.
This can be done more efficiently and cleanly than your present implementation:
set xxx to {"ni", "bu", "en"}
set xxx2 to {"hao", "bu", "ni", "bu", "hao"}
set xxx to xxx & xxx2
return unique(xxx) --> {"ni", "bu", "en", "hao"}
on unique(L)
local L
script
property array : L
property list : {}
end script
tell the result
repeat with x in its array
if x is not in its list then set ¬
end of its list to x's contents
end repeat
its list
end tell
end unique

Related

rspec - how to test for nil input in ruby

I'm trying to write a spec to test how my code will react when a user just presses the "Enter" key i.e. doesn't enter any data just presses "Enter".
The code itself will loop until a valid entry is made but I can't get the spec to test it. The code below is an example of both the class and the spec.
Note that in the spec, I've tried replacing the "asks repeatedly" section with with_input('') but it just seems to hang (or loop)
class Example
def initialize(input: $stdin, output: $stdout)
#input = input
#output = output
end
def ask_for_number
#output.puts "Input an integer 5 or above"
loop do
input = #input.gets.to_i
return true if input >= 5
#output.puts "Invalid. Try again:"
end
end
end
--- And the spec
require 'stringio'
require_relative 'Example'
describe Example do
context 'with input greater than 5' do
it 'asks for input only once' do
output = ask_for_number_with_input(6)
expect(output).to eq "Input an integer 5 or above\n"
end
end
context 'with input equal to 5' do
it 'asks for input only once' do
output = ask_for_number_with_input('5')
expect(output).to eq "Input an integer 5 or above\n"
end
end
context 'with input less than 5' do
it 'asks repeatedly, until a number 5 or greater is provided' do
output = ask_for_number_with_input(2, 3, 6)
expect(output).to eq <<~OUTPUT
Input an integer 5 or above
Invalid. Try again:
Invalid. Try again:
OUTPUT
end
end
def ask_for_number_with_input(*input_numbers)
input = StringIO.new(input_numbers.join("\n"))
output = StringIO.new
example = Example.new(input: input, output: output)
expect(example.ask_for_number).to be true
output.string
end
end
Just mimic the loop:
require "spec_helper"
describe 'Example' do
let(:entered_value) { 6 }
let(:stdin) { double('stdin', gets: entered_value) }
let(:stdout) { double('stdout') }
subject { Example.new(input: stdin, output: stdout) }
describe '#ask_for_number' do
before(:each) do
allow(subject).to receive(:loop).and_yield
end
context 'pressed enter without any input' do
let(:entered_value) { nil }
it 'prints invalid output' do
expect(stdout).to receive(:puts).with("Input an integer 5 or above")
expect(stdout).to receive(:puts).with("Invalid. Try again:")
subject.ask_for_number
end
end
end
end
When you replace it with
output = ask_for_number_with_input("")
it loops forever, because that's what your code tells it do, you wanted it to loop until it receives a number > 6, which will never happen, #input.gets.to_i is just going to keep returning 0 because IO#gets
Returns nil if called at end of file.
To get it to stop hanging, simply give it another value:
it 'asks repeatedly, until a number 5 or greater is provided' do
output = ask_for_number_with_input("", "", 6)
expect(output).to eq <<~OUTPUT
Input an integer 5 or above
Invalid. Try again:
Invalid. Try again:
OUTPUT
end
and now it passes

Read file and write file

Can't seem to get my data to be read as an integer and print out the data. plus there is a close stream (IOError) for count = aFile.gets in def read (afile) function.
This program includes Array, files and loops. The purpose of this program is to take a number 10 and write the number to a file then on each line increment from zero to 10 that is passed.
# takes a number and writes that number to a file then on each line
# increments from zero to the number passed
def write(aFile, number)
# You might need to fix this next line:
aFile.puts("number")
index = 0
while (index < number)
aFile.puts(number.to_s)
index += 1
end
end
# Read the data from the file and print out each line
def read(aFile)
# Defensive programming:
count = aFile.gets
if (is_numeric(count))
count = count.to_i
else
count = 0
puts "Error: first line of file is not a number"
end
index = 0
while (count < index)
line = aFile.gets
puts "Line read: " + line
end
end
# Write data to a file then read it in and print it out
def main
aFile = File.new("mydata.txt", "w") # open for writing
if aFile # if nil this test will be false
write(aFile, 10)
else
puts "Unable to open file to write!"
end
if aFile
read(aFile)
end
aFile.close
end
# returns true if a string contains only digits
def is_numeric?(obj)
if /[^0-9]/.match(obj) == nil
true
end
false
end
main
If you want to make your code work, change:
aFile = File.new("mydata.txt", "w")
to:
aFile = File.new("mydata.txt", "r+")
You can change:
count = aFile.gets
if (is_numeric(count))
to:
count = aFile.gets.to_i
if (count.is_a?(Fixnum))
and then get rid of the is_numeric?(obj) method.
Also you're not incrementing the counter, you can fix that as well.
Here is a skinned code working, you'd easily add the features I removed.
def write(a_file, number)
(1..number).each { |n| a_file.puts(n) }
end
def read(a_file)
a_file.each { |line| puts line }
end
def main
a_file = File.new("mydata.txt", "w")
if a_file
write(a_file, 10)
else
puts "Unable to open file to write!"
end
a_file.close
a_file = File.open("mydata.txt", "r")
if a_file
read(a_file)
end
a_file.close
end
main
The main bugs I've found:
After writing, close the file and open again fro writing
See here for reading: What are all the common ways to read a file in Ruby?
aFile.puts(number.to_s) you should puts index, which is the incrementing variable
(is_numeric(count)) missing ?
Side note: use Ruby notation for variables: a_file is good, aFile is not.

What is wrong with my code in my vbs?

What is wrong with my code?
Option Explicit
Dim obj, x, y, v
set obj=createobject("wscript.shell")
x=inputbox("How many Beefs would you like to test?","WindowsDGK Beef Test")
if not IsNumeric(x) then
msgbox"Please enter a number!"
End If
if IsNumeric(x) then
do
y=y+1
obj.run "cmd.exe"
loop until x=y
v=msgbox("Do you want to run again?","WindowsDGK Beef Test",vbYesNo)
if v=6 then
obj.run wscript.fullname
End If
If v=7 then
wscript.quit
End If
End If
You cannot have 2 variables in an if statement in VBScript. Example: y=(x) won't work because it think the input is a string and not an integer. You really need to edit you post.

Check if variable is number: Applescript

How can I check if a variable is a number?
I'm trying this:
set a to 5
if a is a number
display dialog "Yes! It's a number!"
end if
I've also tried this code:
set a to 5
if a is integer
display dialog "Yes! It's a number!"
end if
But unfortunately it doesn't work as expected.
set a to 5
if class of a is integer then
display dialog "Yes! It's a number!"
end if
class of a is integer will fail if you use
set a to "5"
This will work if even if the variable is a number but was entered as text.
set a to "5"
try
set a to a as number
display dialog "Yes! It's a number!"
end try
This is my solution:
on is_number(number_string)
try
set number_string to number_string as number
return true
on error
return false
end try
end is_number

AppleScript syntax

I know this is sloppy code, but here it is:
display dialog ("Start Screensaver. Please type: matrix, coffee, waffles, star, water, or
fireworks.", default answer "")
if text returned of result = "matrix" then
set user_choice to "MatrixSaver"
else
if text returned of result = "coffee" then
set user_choice to "Coffee"
else
if text returned of result = "waffles" then
set user_choice to "Waffles"
else
if text returned of result = "star" then
set user_choice to "Hyperspace"
else
if text returned of result = "water" then
set user_choice to "LotsaWater"
else
if text returned of result = "fireworks" then
set user_choice to "Skyrocket"
else
(*do nothing*)
end if
end if
end if
end if
end if
end if
if (user_choice = null) then (*do nothing*)
else
tell application "System Events"
set ss to screen saver user_choice
start ss
end tell
end if
When I'm trying to compile my code, the 'default answer' Is highlighted, and it says: "Expected “)”, etc. but found identifier."
Any Ideas? Thanks.
I believe the correct syntax is just
display dialog "Start Screensaver. Please ..." default answer ""
The , between the ("Start Screensaver") and the default answer parameter is causing the syntax error. Remove the ,.
This isn't a syntax error, but the variable user_choice doesn't exist outside of the big if block. If you ran it as written, you would get this message at the last if block:
The variable user_choice is not defined.
You could fix this by declaring the variable before the display dialog statement...
set the user_choice to ""
Now you can use the variable anywhere in the code. :)

Resources