2013-07-11 5 views
1

나는 스크립트가 실행될 때마다 카운트에 하나를 더하는 간단한 카운터를 만들려고한다. 속성을 사용하여 시도했지만 스크립트가 편집되거나 컴퓨터가 꺼질 때마다 다시 설정되기 때문에 작동하지 않습니다. 여기 내가 가지고있는 코드는 here에서 가져온 것입니다. 이와 숫자 변수를 AppleScript로 파일에 저장하는 방법은 무엇입니까?

set theFile to ":Users:hardware:Library:Scripts:Applications:LightSpeed:" & "CurrentProductCode.txt" 

open for access theFile 
set fileContents to read theFile 
close access theFile 

set counter to fileContents as integer 

on add_leading_zeros(counter, max_leading_zeros) 
    set the threshold_number to (10^max_leading_zeros) as integer 
    if counter is less than the threshold_number then 
     set the leading_zeros to "" 
     set the digit_count to the length of ((counter div 1) as string) 
     set the character_count to (max_leading_zeros + 1) - digit_count 
     repeat character_count times 
      set the leading_zeros to (the leading_zeros & "0") as string 
     end repeat 
     return (leading_zeros & (counter as text)) as string 
    else 
     return counter as text 
    end if 
end add_leading_zeros 

add_leading_zeros(counter, 6) 


open for access newFile with write permission 
set eof of newFile to 0 
write counter + 1 to newFile 
close access newFile 

내가 오류를 얻을 :

Can’t make ":Users:hardware:Library:Scripts:Applications:LightSpeed:CurrentProductCode.txt" into type file.

내가 처음 "액세스 theFile 오픈"후 코드에 조금 더 얻는다 "별칭으로 theFile에 theFile 설정"을 추가하는 경우, 다른 오류가 발생합니다.

Can’t make "1776" into type integer.

이제 아이디어가 없습니다. 나는 그 곳의 모든 것을 봤다. 나를 위해 일하는 것을 찾지 못했다. 내가 스크립트를 사용하려면 감사

답변

2

는 데이터를 저장하는 객체 :

set thePath to (path to desktop as text) & "myData.scpt" 

script theData 
    property Counter : missing value 
end script 

try 
    set theData to load script file thePath 
on error 
    -- On first run, set the initial value of the variable 
    set theData's Counter to 0 
end try 

--Increment the variable by 1 
set theData's Counter to (theData's Counter) + 1 

-- save your changes 
store script theData in file thePath replacing yes 
return theData's Counter 
+0

신난다! 감사. 완벽하게 작동합니다. 숫자 변수를 저장하는 더 좋은 방법이 있어야한다는 것을 알고있었습니다! 다시 한 번 감사드립니다. –

관련 문제