applescript variable from loop
Hi, I have a script which works with .txt file and takes a numbers, do some with it in the loop and must create a new file where save results of calculation. Now my script can open a file, see a words in it, add some number to words. But I do not knew how can I send results from loop to another file or as result. For example, loop works with file, which have four numbers as text. Loop takes one number, second, third but as result remember fourth result only.
Open a txt file It can't, because file already open and loop takes a numbers from it, if I try to create a second file, applescript takes mistake.
Please, help
loop in the bottom of script
set thisFile to choose filewith prompt "Choose an image file: "
tell application "iMagine Photo"
set thisImporter to import graphicthisFile
if the component error of thisImporter is not equal to 0 then
closethisImporter
display dialog "Not an image file that quicktime recognizes."
return
end if
set {x, y, xDim, yDim} to the natural bounds of thisImporter
set thisDocument to makenewwindow documentwith properties {dimensions:{xDim, yDim}}
set the drawing destination of thisImporter to thisDocument
drawthisImporter
closethisImporter
-- The following section is to be replaced depending on the pixel value class
set pixelValueRect to {x, y, xDim, yDim}
set pixelValues1 to get pixel values of thisDocumentwith properties {pixel value class:pixels in rectangle, bounds rectangle:pixelValueRect}
closethisDocument
--class (pixel points) as list
--"class pCol" as list -- цвета и координаты разнесены по классам с именами, приводим к списку
end tell
tell application "iMagine Photo"
-- set pixelValues1 to ...
set t to my to_s(pixelValues1)
end tell
tell application "TextEdit"
tell (makenewdocument)
set its text to t
end tell
end tell
on to_s(a)
(*
a: anything
return Unicode text: text representation of a
*)
local astid0
set astid0 to AppleScript'stext item delimiters
try
{«class AAAA»:a}'s «class BBBB»
on error t-- t = "Can't get «class BBBB» of {«class AAAA»:a}"
try
set AppleScript'stext item delimiters to {"{«class AAAA»:"}
set t to t'stext items 2 thru -1 as Unicode text
set AppleScript'stext item delimiters to {"}"}
set t to t'stext items 1 thru -2 as Unicode text
set AppleScript'stext item delimiters to astid0
return t-- t = a, which is in Unicode text
on error errsnumbererrn
set AppleScript'stext item delimiters to astid0
error errsnumbererrn
end try
end try
end to_s
tell application "TextEdit"
tell front document
tell its text-- NB. 'its'. We need the TextEdit reference, not the text itself.
-- delete (paragraphs -3 thru -1)
set noneedenttext to (offsetof "pixel points" in (it as text)) + 13 -- 'it as text' because 'offset' does need the actual text.
set noneedenttext1 to (offsetof "pixel colors" in (it as text)) - 2 -- 'it as text' because 'offset' does need the actual text.
set noneedenttext2 to (offsetof "pixel colors" in (it as text)) - 16
delete (characters 1 thru noneedenttext)
delete (charactersnoneedenttext1 through noneedenttext2)
set nonech to (offsetof "}}" in (it as text)) --определяем лишние скобки за координатами
set nonech1 to (offsetof "}}}" in (it as text)) --определяем лишние скобки за цветами
delete (characternonech) --стираем нахуй
delete (characters (nonech1 - 1) through nonech1) --стираем нахуй
display dialog "введите количество цветов в картинке от 2 до 12" default answer "4" buttons {"OK"} default button 1
copy the result as list to {button_res, value_res}
if button_res is "OK" then
set colors_inpic to value_res
--set words of doc whose it is keyword to keyword_repl
set color_span to 65000 / colors_inpic--определяем на сколько делить цифру цвета для упрощения количества цветов
end if
set word_count to countwords of (it as text as string)
set character_count to countcharacters of (it as text as string)
set pixel_amount to word_count / 5 as number--количество пискселей
set word_pixels to word_count / 2.5 --количество слов-координат
set pixel_data to words 1 thru word_pixels-- as string если оставить, пишет все в ряд как одно число
set pixel_col to words (word_pixels + 1) thru -1 -- +1 значит без последнего слова, -1 знаит до конца
set counter to 1
set ad_i to 1
--tell (make new document)
repeat pixel_amount times
set x to word (counter) as integer
set y to word (counter + 1) as integer
set r to ((word ((word_pixels + ad_i))) / color_span) as integer
set g to ((word ((word_pixels + ad_i + 1))) / color_span) as integer
set b to ((word ((word_pixels + ad_i + 2))) / color_span) as integer
set full_pixel to x & " " & y & " " & r & " " & g & " " & b
set counter to counter + 2
set ad_i to ad_i + 3
display alert "" & full_pixel
--set its text to full_pixel
end repeat
--end tell
end tell
end tell
end tell
MacBook Air, OS X Yosemite (10.10.3)