2014-02-26 2 views
0

서언하려면 : My Apple 스크립트는 Excel 파일을 가져 와서 데이터를 새로운 일반 텍스트 파일에 복사합니다.AppleScript : 이전 파일 이름이없는 새 파일 쓰기

원본 파일의 파일 이름없이 기존 Excel 파일과 동일한 위치에 새 텍스트 파일을 쓰는 데 문제가 있습니다.

예를 들어

: 내 엑셀 파일은 "FilenameA.xls"내가 내 새 텍스트 파일을 저장할 때, 그것은 "FilenameA.xlsFilenameB.txt"나는 같은이 새 파일을 저장할 수있는 방법

로 저장 위치,하지만 내 자신의 파일 이름?

참고 : (path to desktop as text)을 사용할 때 작동하지만, 항상 내 데스크톱에 저장하지는 않습니다.

set myFile to open for access (path to desktop as text) & blahBlah & ".txt" with write permission 

아래의 스크립트를 시도하면 원본 파일 이름에 새로운 파일 이름을 추가합니다.

set myFile to open for access (fileName) & blahBlah & ".txt" with write permission 

참고 : fileName은 원본 Excel 파일의 경로를 나타냅니다.

기본적으로 (path to desktop as text)과 동일한 결과를 원하지만 원본에 액세스 한 폴더에 파일을 저장할 수있는 유연성이 필요합니다.

답변

1

요청 해 주셔서 감사합니다. 기본적으로 코드에 "컨테이너의"줄을 추가하고이를 새 파일 경로 이름에 사용해야했습니다. 더 많은 코드를 게시 한 경우 구문을 적용 할 수는 있지만 충분히 이해해야합니다. 다음은 엑셀 파일을 선택할 수있는 몇 가지 샘플 코드,의는 새 파일 이름에 대한 메시지를 표시, 해당 파일의 컨테이너를 수집 한 다음 새 txt 파일 작성할 수 있습니다 : 나는 몇 가지있다

set excelFile to (choose file with prompt "Choose Excel file :" of type {"xls", "xlsx"} without invisibles) 

tell application "Finder" 
    set containerPath to (container of excelFile) as string 
    set oldFilename to name of excelFile 
end tell 

set newFilename to text returned of (display dialog "Enter name of the resulting text file" default answer oldFilename) 

set myFile to open for access (containerPath & newFilename & ".txt") with write permission 
try 
    -- add content to myFile 
    write "Here's some groovy content added to the file." to myFile 
    close access myFile 
on error 
    close access myFile 
end try 
+0

내 문제는 내가 새로운 저장 원본 파일의 경로를 사용하고 있다는 것입니다 :

또한 기존 파일을 열고 그것의 끝에 텍스트를 추가하고 다시 저장하는 텍스트 편집기를 사용할 수 있습니다 파일. 'open for access'와 함께 경로를 사용하면 원래 파일 이름이 내 사용자 지정 매개 변수 앞에 놓입니다. 새 파일이 내가 선택한 파일 이름을 지니고 싶습니다. – pianoman

+0

나는 나의 의도를 잘 반영하기 위해 질문을 편집했다. 그게 도움이 되니? – pianoman

+0

예, 도움이되는 설명입니다.수정 된 답변의이 샘플 코드는 당신을 위해 해결해야합니다. – jweaks

0

을 위의 해결 방법에 대한 개선 제안.

set oldFilename to displayed name of excelFile 

그런 식으로, 당신은 "Spreadsheet.xlsx"와 "스프레드 시트로 끝날 것입니다 : 당신이 Excel 파일의"표시 이름 "을 사용하는 경우

는, 파일 확장자를 포함하지 않습니다. txt "가 아닌"Spreadsheet.xlsx.txt "와 같은 폴더에 저장하십시오.

TextEdit에서 텍스트 파일을 쓸 수 있기 때문에 열려있는 상태로 자신의 텍스트 파일을 작성할 필요가 없습니다.

tell application "TextEdit" 
    make new document with properties {text:"The text file contents."} 
    close document 1 saving in file (containerPath & newFilename & ".txt") 
end tell 

그런 식으로 TextEdit의 정교함을 텍스트 파일과 함께 활용하고 있습니다. UTF-8 텍스트 파일을 만들면 쓰기 액세스 또는 다른 저수준 디스크 오류로 인해 파일을 열어 둘 위험이 없습니다.

tell application "TextEdit" 
    open file (containerPath & newFilename & ".txt") 
    set the text of document 1 to the text of document 1 & return & "Some more text." 
    close document 1 saving yes 
end tell 
관련 문제