현재 직면한 문제
현재 macOS App에서 applescript로 numbers app을 실행시키면 아래와 같은 오류가 발생합니다.
AppleScript 실행 오류: {
NSAppleScriptErrorAppName = Numbers;
NSAppleScriptErrorBriefMessage = "Application isn\\U2019t running.";
NSAppleScriptErrorMessage = "Numbers got an error: Application isn\\U2019t running.";
NSAppleScriptErrorNumber = "-600";
NSAppleScriptErrorRange = "NSRange: {30, 8}";
}
error -600 해결 방법 케이스
첫번째 방법 (실패)
info.plist에 NSappleEventsUsageDescription 키를 추가 하지만 에러 -600 발생
Got "Application isn’t running. (error -600)" when executing applescript in swift
OS: MacOS Catalina 10.15.1 Language: Swift IDE: Xcode I'm trying to run an apple script with module AppleScriptObjC in my macOS app, and the apple script code is showed below: -- test.applescript
stackoverflow.com
두번째 방법 (실패)
NSAppleScript 직접 호출 방법 에러 -600 발생
How to open single "Get Info window" for several/multiple files/dirs?
I have a code: func openGetInfoWnd(for urls: [URL]) { let pBoard = NSPasteboard(name: NSPasteboard.Name(rawValue: "pasteBoard_\(UUID().uuidString )") ) pBoard.writeObjects(ur...
stackoverflow.com
let myAppleScript = "number app 실행 후 변환 코드"
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: myAppleScript) {
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
&error) {
print(output.stringValue)
} else if (error != nil) {
print("error: \\(error)")
}
}
세번째 방법 (실패)
셸을 통해 애플스크립트를 실행, numbers app 실행은 에러 발생 ❌
하지만 sheet 생성 후 image 파일을 넣어 줄 때 에러 -600 발생
Can you execute an Applescript script from a Swift Application
I have a simple AppleScript that sends an email. How can I call it from within a Swift application? (I wasn't able to find the answer via Google.)
stackoverflow.com
다른 해결 방안
서버 통신 (진행중, 가능성 매우 높음)
이때 Network 클라이언트 권한 설정: "App Sandbox" 섹션에서 "Network" 카테고리에 있는 "Outgoing Connections (Client)" 옵션을 활성화합니다.

1. AppleScript 파일을 Xcode안에 넣고 실행 시키면 sandbox 제거 안해도 실행 가능
참고자료 https://stackoverflow.com/questions/61988694/how-to-run-applescript-from-inside-a-swiftui-view
How to run AppleScript from inside a SwiftUI View?
I'm kind of getting some more understanding with basic SwiftUI but now I wanted to extend my application to actually do some stuff regarding system components for me. Basically I want to run an
stackoverflow.com
2. AppleScript내에서 url 통신을 curl을 통하여 json파일, 변수 등 가져오기
-- API 호출을 위한 URL 설정
set serverURL to "<https://rss.applemarketingtools.com/api/v2/us/music/most-played/10/albums.json>"
-- curl 명령을 구성하기 위한 명령어 문자열 설정
set curlCommand to "curl " & quoted form of serverURL
try
-- curl 명령을 사용하여 서버에 요청하고, 응답을 받아옴
set serverResponse to do shell script curlCommand
-- 받아온 데이터 처리
set AppleScript's text item delimiters to "," -- 텍스트 항목 구분자를 쉼표로 설정
set albumNames to every text item of serverResponse -- 쉼표로 문자열을 나누어 배열로 저장
-- 앨범 목록을 다이얼로그로 표시
set albumNamesString to albumNames as string
display dialog "Most played albums:" & return & albumNamesString
on error errMsg
-- 예외 발생 시 에러 메시지를 다이얼로그로 표시
display dialog "Error: " & errMsg
end try
3. Parsing JsonData
사실 AppleScript 자체로 Json파일을 Parsing하는 것은 매우 효율이 안 좋은 일이다.
리서치 결과 javascript와 python을 많이들 이용하지만 이번 작업에서 필요로 하는 applescript로만 parsing을 하는 코드를 작성하였다.
- AppleScript내에서 직접 서버와 통신하여 Parsing 하는 방법
set serverURL to "<https://koreanjson.com/posts/1>"
set curlCommand to "curl " & quoted form of serverURL
try
set serverResponse to do shell script curlCommand
-- Process the received data
set AppleScript's text item delimiters to ","
set json to every text item of serverResponse
-- json파일을 넣어주기
set koreanjson to json as string
set jsonString to koreanjson
set AppleScript's text item delimiters to ","
set idText to (text item 2 of jsonString)
set titleText to (text item 4 of jsonString)
set contentText to (text item 6 of jsonString)
set createdAtText to (text item 8 of jsonString)
display dialog "id: " & idText & ", title: " & titleText & ", content: " & contentText & ", createdAt: " & createdAtText
on error errMsg
display dialog "Error: " & errMsg
end try
프로그램 파일 안에서 txt파일로 변수 전달 (진행중)
'AppleScript' 카테고리의 다른 글
AppleScript (2) | 2023.10.02 |
---|