I’m trying to get the paragraph after the user selected text but I keep stumbling. Any help would be appreciated. Here’s my code so far:
tell application "Xcode"
-->find the current document name
set my_work_space to active workspace document
set my_projects to projects of my_work_space
set my_project to item 1 of my_projects
set my_windows to windows
set my_window to item 1 of my_windows
set window_name to name of my_window
-->get the current document
tell me
set my_names to split(window_name, " ” ")
end tell
set target_file_name to item 2 of my_names
set itemNumber to 0
repeat with a_document in text documents
set itemNumber to itemNumber + 1
if ((name of a_document as string) is (target_file_name as string)) then
exit repeat
end if
end repeat
-->get the current document
set sourceDocument to item itemNumber of every text document of application "Xcode"
-->and it's path
set sourcePath to path of sourceDocument
-->read the source document
set sourceTxt to my readFile(sourcePath) as text
-->make sure something was selected
set selectedRange to the selected character range of sourceDocument
if first item of selectedRange > second item of selectedRange then
display dialog "Nothing selected"
return -999
else
set endCharacter to item 2 of selectedRange
-->and the wheels come off here... : D
end if
end tell
--Methods--
to split(aString, delimiter)
set oldastid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set pieces to aString's text items
set AppleScript's text item delimiters to oldastid
return pieces
end split
on readFile(filePath)
set foo to (open for access (POSIX file filePath))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile
I resolved it. But a different problem has come up.
When I log this line:
-->make sure something was selected
set selectedRange to the selected character range of sourceDocument
I get {291, 291} (it’s an empty line)
However, when I run this code (where I commented on the wheels coming off):
set endCharacterNum to item 2 of selectedRange
set characterCount to 0
repeat with x from 1 to count of every paragraph of sourceTxt
set thisParagraph to paragraph x of sourceTxt
set characterCount to characterCount + (count of every character of thisParagraph)
log thisParagraph & "::" & characterCount & "::" & x
if characterCount > endCharacterNum then
set nextParagraph to paragraph (x + 2) of sourceTxt
exit repeat
end if
end repeat
my log shows me as the same line starting with char 275
(*//::2::1*)
(*// OAI_SetTabOrder.h::23::2*)
(*// avi::30::3*)
(*//::32::4*)
(*// Created by Steve Suranie on 4/19/13.::72::5*)
(*// Copyright (c) 2013 Olympus. All rights reserved.::124::6*)
(*//::126::7*)
(*::126::8*)
(*#import <Foundation/Foundation.h>::159::9*)
(*#import "OAI_TextField.h"::184::10*)
(*::184::11*)
(*@interface OAI_SetTabOrder : NSObject::221::12*)
(*::221::13*)
(*@property (nonatomic, retain) NSArray* arrAllElements;::275::14*)
(*::275::15*)
(*::275::16*)
(*- (NSMutableArray*) setTabOrder; ::308::17*)
So I am getting paragraph 18, which is @end when what I am looking for is the NSMutableArray paragraph. Not sure why I am getting the discrepency or how to resolve it.
My guess is that I am not working apples to apples here - sourceTxt is the contents of sourceDocument but perhaps under the hood in XCode sourceDocument comes with header material that is pushing the character count to 291 instead of 275.
What I am trying to do is automate the placement of Doxygen comments for documenting my app code. You place the comments in the .h files in this format
/**
this method does something wonderful and takes a NSString and returns a BOOL @param NSString @returns BOOL
*/
and the differences are not the same, they vary on different files.
OK, so I got it working thanks to the help from here except one major flaw, it’s not writing to my file. Here’s the whole code Any ideas why this won’t work?:
tell application "Xcode"
-->find the current document name
set my_work_space to active workspace document
set my_projects to projects of my_work_space
set my_project to item 1 of my_projects
set my_windows to windows
set my_window to item 1 of my_windows
set window_name to name of my_window
-->get the current document
tell me
set my_names to my split(window_name, " ” ")
end tell
set target_file_name to item 2 of my_names
set itemNumber to 0
repeat with a_document in text documents
set itemNumber to itemNumber + 1
if ((name of a_document as string) is (target_file_name as string)) then
exit repeat
end if
end repeat
-->get the current document
set sourceDocument to item itemNumber of every text document of application "Xcode"
-->and it's path
set sourcePath to path of sourceDocument
-->read the source document
set sourceTxt to my readFile(sourcePath) as text
-->make sure something was selected
set selectedRange to the selected character range of sourceDocument
if first item of selectedRange > second item of selectedRange then
display dialog "Nothing selected"
return -999
else
set endCharacterNum to item 2 of selectedRange
set characterCount to 0
repeat with x from 1 to count of every paragraph of sourceTxt
set thisParagraph to paragraph x of sourceTxt
set characterCount to characterCount + (count of every character of thisParagraph) + 1
if characterCount > endCharacterNum then
set nextParagraph to paragraph (x) of sourceTxt
exit repeat
end if
end repeat
end if
-->start doxygen comment
set doxyComment to return & "//*" & return & return
-->get parameters for method
set valuesList to my split(nextParagraph, "(")
if (count of every item of valuesList) > 2 then
repeat with i from 1 to count of every item of valuesList
if i > 2 then
set paramObject to my split(item i of valuesList, ")")
set thisParam to item 1 of paramObject
-->strip pointer if param is not a primitive
if thisParam contains "*" then
set thisParamList to my split(thisParam, "*")
set thisParam to item 1 of thisParamList
end if
set doxyComment to doxyComment & "@param " & thisParam & return
end if
end repeat
end if
-->get returned item
set returnListEnd to my split(item 2 of valuesList, ")")
set returnItem to item 1 of returnListEnd
if returnItem is not equal to "void" then
set doxyComment to doxyComment & "@return " & returnItem & return
end if
-->close doxycomment
set doxyComment to doxyComment & "*/"
-->insert doxyComment in code
set newText to ""
-->make new page text
repeat with i from 1 to count of every paragraph of sourceTxt
if i is not equal to x - 1 then
set newText to newText & contents of paragraph i of sourceTxt
else
set newText to newText & doxyComment
end if
end repeat
set sourcePath to sourcePath
-->write text to file
try
open for access sourcePath as POSIX file with write permission
write newText to file sourcePath starting at eof
close access sourcePath
on error --you never know when errors will crop up (when it comes to reading and writing files); better to be safe than sorry
try
close access the_file
end try
end try
end tell
--Methods--
on split(aString, delimiter)
set oldastid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set pieces to aString's text items
set AppleScript's text item delimiters to oldastid
return pieces
end split
on readFile(filePath)
set foo to (open for access (POSIX file filePath))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile
OK, resolved the writing to file issue. I was trying to open the file when it was already open. New issue is to fix the parsing - all the returns are getting wiped out when I get the contents of the file and all comments written as so: //this is a comment are being rewritten ////this is a comment
New code dump:
tell application “Xcode”
-->find the current document name
set my_work_space to active workspace document
set my_projects to projects of my_work_space
set my_project to item 1 of my_projects
set my_windows to windows
set my_window to item 1 of my_windows
set window_name to name of my_window
-->get the current document
tell me
set my_names to my split(window_name, " ” ")
end tell
set target_file_name to item 2 of my_names
set itemNumber to 0
repeat with a_document in text documents
set itemNumber to itemNumber + 1
if ((name of a_document as string) is (target_file_name as string)) then
exit repeat
end if
end repeat
-->get the current document
set sourceDocument to item itemNumber of every text document of application "Xcode"
-->and it's path
set sourcePath to path of sourceDocument
-->read the source document
set sourceTxt to my readFile(sourcePath) as text
-->make sure something was selected
set selectedRange to the selected character range of sourceDocument
if first item of selectedRange > second item of selectedRange then
display dialog "Nothing selected"
return -999
else
set endCharacterNum to item 2 of selectedRange
set characterCount to 0
repeat with x from 1 to count of every paragraph of sourceTxt
set thisParagraph to paragraph x of sourceTxt
set characterCount to characterCount + (count of every character of thisParagraph) + 1
if characterCount > endCharacterNum then
set nextParagraph to paragraph (x) of sourceTxt
exit repeat
end if
end repeat
end if
-->start doxygen comment
set doxyComment to return & "//*" & return & return
-->get parameters for method
set valuesList to my split(nextParagraph, "(")
if (count of every item of valuesList) > 2 then
repeat with i from 1 to count of every item of valuesList
if i > 2 then
set paramObject to my split(item i of valuesList, ")")
set thisParam to item 1 of paramObject
-->strip pointer if param is not a primitive
if thisParam contains "*" then
set thisParamList to my split(thisParam, "*")
set thisParam to item 1 of thisParamList
end if
set doxyComment to doxyComment & "@param " & thisParam & return
end if
end repeat
end if
-->get returned item
set returnListEnd to my split(item 2 of valuesList, ")")
set returnItem to item 1 of returnListEnd
if returnItem is not equal to "void" then
set doxyComment to doxyComment & "@return " & returnItem & return
end if
-->close doxycomment
set doxyComment to doxyComment & "*/"
-->insert doxyComment in code
set newText to ""
-->make new page text
repeat with i from 1 to count of every paragraph of sourceTxt
if i is not equal to x - 1 then
set newText to newText & contents of paragraph i of sourceTxt
else
set newText to newText & doxyComment
end if
end repeat
-->write text to file
set sourcePath to sourcePath as string
set insertionPoint to 0
write newText to sourcePath starting at insertionPoint
close access sourcePath
end tell
–Methods–
on split(aString, delimiter)
set oldastid to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to delimiter
set pieces to aString’s text items
set AppleScript’s text item delimiters to oldastid
return pieces
end split
on readFile(filePath)
set foo to (open for access (POSIX file filePath))
set txt to (read foo for (get eof foo))
close access foo
return txt
end readFile
I wonder why you use Return and not Linefeed in the first place. As LineFeed would be the default text ending in a source-code text. (Interesting script by the way.)
I saw the field for selecting what kind of line-endings you want in your file, in XCode, so if return is what you want, I think your script will not neglect returns, if you have chosen return as the line-ending in the XCode options.
I was in to see if I could fix the brace expansion, I haven’t got that to work quite correctly for me when I am editing code. I can’t figure out what to do about it.