2011-09-12 3 views
1

저는 방금 xcode에서 applescript로 시작하고 현재 폴더 위치를 묻는 응용 프로그램을 가지고 있으며 폴더 구조를 만듭니다. 하나의 변수를 애플 스크립트로 쓰거나 읽으십시오.

on buttonClick_(sender) 
    set theLocation to choose folder with prompt "Where to save your project?" 
    tell application "Finder" 
     set newFolder to make new folder at theLocation with properties {name:(theTextField's stringValue() as string)} 
     set fontsFolder to make new folder at newFolder with properties {name:"fonts"} 
     set jpgFolder to make new folder at newFolder with properties {name:"jpg-pdf"} 
     set mainFolder to make new folder at newFolder with properties {name:"main"} 
     set printFolder to make new folder at mainFolder with properties {name:"• for printer"} 
     set refverFolder to make new folder at newFolder with properties {name:"ref_ver"} 
     set supportFolder to make new folder at newFolder with properties {name:"support"} 
    end tell 
    quit 
end buttonClick_ 

는 이제 앱 받아 "theLocation"폴더 별칭을하고 저장하기 위해 노력하고있어, 그래서 응용 프로그램이 자동으로 시작하는 다음 번에 추가 할 필요없이 저장 위치로 해당 폴더를 선택합니다. 나는 그 안에 들어갈 논리를 이해하지만 정보를 저장/읽는 법을 이해할 수는 없습니다. 나는 info.plist에 글쓰기에 관한 튜토리얼을 시도했지만, 그들 중 누구도 일하지 않았다. 내가 어떻게 애플 스크립트가 작동하는지에 대한 기본적인 정보가 빠졌습니까?

감사

** 편집 당신은 스크립트가 종료 된 후에도 정보를 저장할 수 있도록 스크립트의 시작 부분에 속성을 정의 할 수 있습니다

+0

나는 사과 스크립트에 초보자입니다. info.plist를 편집 할 수있는 방법을 알려주시겠습니까? info.plist 파일의 일부 속성을 내 값으로 편집하고 싶습니다. 도움이 될 것입니다. – user1010819

답변

2

속성은 Xcode 스크립트에 지속되지 않지만 user defaults 시스템을 사용할 수 있습니다. 기본값을 사용하려면 응용 프로그램이 시작될 때 일부 초기 값을 등록한 다음 기본값 (이전에 저장 한 경우 등록 된 값을 덮어 씁니다)에서 읽고 응용 프로그램이 종료 할 때 새 값을 저장합니다 (예 :

).
property theLocation : "" -- this will be the (text) folder path 

on applicationWillFinishLaunching_(aNotification) 
    tell standardUserDefaults() of current application's NSUserDefaults 
     registerDefaults_({theLocation:theLocation}) -- register the starting user default key:value items 
     set theLocation to objectForKey_("theLocation") as text -- read any previously saved items (which will update the values) 
    end tell 
    -- other initialization stuff 
end applicationWillFinishLaunching_ 

on buttonClick_(sender) 
    if theLocation is "" then 
     set theLocation to choose folder with prompt "Where to save your project?" 
     set theLocation to theLocation as text 
    end if 
    -- display dialog theLocation 
    tell application "Finder" 
     -- create folder structure 
    end tell 
    quit 
end buttonClick_ 

on applicationShouldTerminate_(sender) 
    tell standardUserDefaults() of current application's NSUserDefaults 
     setObject_forKey_(theLocation, "theLocation") -- update the default items 
    end tell 
    return current application's NSTerminateNow 
end applicationShouldTerminate_ 
+0

이것은 내가 찾고있는 방향과 정확히 같지만 응용 프로그램에서는 Location을 열 때 기본값이있는 것으로 인식하지 못합니다. NSUserDefaults를 사용하려면 어디서든 초기화하거나 클래스를 가져와야합니까? 내 전체 코드를 원래 게시물에 추가하겠습니다. –

+0

NSUserDefaults는 속성 목록에 표시 할 수있는 객체 만 지원하므로 별칭의 경우 NSData 객체를 만들어야합니다. 아니면이 경우에는 폴더 경로를 텍스트로 강제 변환합니다. 이 예제에서 buttonClick_ 핸들러를 업데이트하여 경로 강제 변환을 보여줍니다 (디버그 콘솔에서도 이와 같은 plist 오류가 표시됩니다). –

+0

나는 그것을 지금 본다! 위치를 이미 '선택 폴더'가 발행 된 후에 텍스트로 설정하는 대신 아무것도 수행하지 않은 기본 위치에있는 경우에만 별칭을 텍스트로 설정했습니다. 지금 일하고 있으며 도움을 주셔서 감사합니다. –

0

script New_ProjectAppDelegate 
property parent : class "NSObject" 

property theTextField : missing value 
property theLocation : "" 

on applicationWillFinishLaunching_(aNotification) 
    tell standardUserDefaults() of current application's NSUserDefaults 
     registerDefaults_({theLocation:theLocation}) -- register the starting user default key:value items 
     set theLocation to objectForKey_("theLocation") as text -- read any previously saved items (which will update the values) 
    end tell 
end applicationWillFinishLaunching_ 

on buttonClick_(sender) 
    if theLocation is "" then 
     set theLocation to choose folder with prompt "Where to save your project?" 
     tell standardUserDefaults() of current application's NSUserDefaults 
      setObject_forKey_(theLocation, "theLocation") -- update the default items 
     end tell 
    else 
     set theLocation to theLocation as text 
     --display dialog theLocation as text 
    end if 
    tell application "Finder" 
     set newFolder to make new folder at theLocation with properties {name:(theTextField's stringValue() as string)} 
     set fontsFolder to make new folder at newFolder with properties {name:"fonts"} 
     set jpgFolder to make new folder at newFolder with properties {name:"jpg-pdf"} 
     set mainFolder to make new folder at newFolder with properties {name:"main"} 
     set printFolder to make new folder at mainFolder with properties {name:"• for printer"} 
     set refverFolder to make new folder at newFolder with properties {name:"ref_ver"} 
     set supportFolder to make new folder at newFolder with properties {name:"support"} 
    end tell 
    quit 
end buttonClick_ 


on applicationShouldTerminate_(sender) 
    tell standardUserDefaults() of current application's NSUserDefaults 
     setObject_forKey_(theLocation, "theLocation") -- update the default items 
    end tell 
    return current application's NSTerminateNow 
end applicationShouldTerminate_ 

종료 스크립트 ..

"theLocation"에 별칭을 저장합니다. 향후 사용을 위해. 그러나 스크립트를 저장하거나 다시 컴파일하면 속성이 원래 값으로 다시 설정됩니다.

관련 문제