在ApplescriptObjc Xcode项目中使用Applescript遇到麻烦

tdnelson2

我是Applescript-ObjC的新手,正在尝试做一些非常简单的事情。我已经使用了这个applescript应用程序,用于将文本转换为Title Case和ALL CAPS。该应用程序很好用。但是,当我尝试将其带入Xcode AppDelegate.applescript并将其附加到UI上的按钮时,什么也没发生。我运行了“ Build”,没有错误,但是当我打开应用程序并单击按钮时,它根本不执行任何操作。

任何帮助将不胜感激。提前致谢。

(顺便说一句,此脚本来自http://macscripter.net/viewtopic.php?pid=42284#p42284

script AppDelegate
property parent : class "NSObject"
-- IBOutlets

on applicationWillFinishLaunching_(aNotification)
    -- Insert code here to initialize your application before any files are opened 
end applicationWillFinishLaunching_


on applicationShouldTerminate_(sender)
    -- Insert code here to do any housekeeping before your application quits 
    return current application's NSTerminateNow
end applicationShouldTerminate_

############# My Buttons #############

-- Title Case --

on clickTitleCase_(sender)
    set someText to the clipboard

    set newCase to (changeCase of someText to "title") (* "upper", "lower", "sentence", "capitalized" or "title" *)

    set the clipboard to newCase
end clickTitleCase_

-- All CAPS --

on clickALLCAPs_(sender)
    set someText to the clipboard

    set newCase to (changeCase of someText to "upper") (* "upper", "lower", "sentence", "capitalized" or "title" *)

    set the clipboard to newCase
end clickALLCAPs_

############# Text Conversion Handlers #############

property lowerStr : "abcdefghijklmnopqrstuvwxyzáàâäãåæçéèêëíìîïñóòôöõōøœúùûüÿ"
property upperStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÂÄÃÅÆÇÉÈÊËÍÌÎÏÑÓÒÔÖÕŌØŒÚÙÛÜŸ"
property alphaList : lowerStr's characters & reverse of upperStr's characters
property sentenceBreak : {".", "!", "?"}
property wordBreak : {space, ASCII character 202, tab}
property everyBreak : wordBreak & sentenceBreak
property whiteSpace : wordBreak & {return, ASCII character 10}
property currList : missing value
property sentenceModList : {"i", "i'm", "i’m", "i've", "i’ve", "I’ve", "I've", "I’m", "I'm", "I"} (* could be extended to include certain proper nouns, acronyms, etc. *)
property mixedModList : {"A", "For", "So", "An", "In", "The", "And", "Nor", "To", "At", "Of", "Up", "But", "On", "Yet", "By", "Or", "Usa", "Nasa", "Hiv", "Aids", "Vs", "Pm", "Wef", "Wwf", "Nsx", "NSX", "WWF", "WEF", "PM", "vs", "AIDS", "HIV", "NASA", "USA", "or", "by", "yet", "on", "but", "up", "of", "at", "to", "nor", "and", "the", "in", "an", "so", "for", "a"}

on textItems from currTxt
    tell (count currTxt's text items) to if it > 4000 then tell it div 2 to return my (textItems from (currTxt's text 1 thru text item it)) & my (textItems from (currTxt's text from text item (it + 1) to -1))
    currTxt's text items
end textItems

on initialCap(currTxt)
    tell currTxt to if (count words) > 0 then tell word 1's character 1 to if it is in lowerStr then
    set AppleScript's text item delimiters to it
    tell my (textItems from currTxt) to return beginning & upperStr's character ((count lowerStr's text item 1) + 1) & rest
end if
currTxt
end initialCap

to capItems from currTxt against breakList
repeat with currBreak in breakList
    set text item delimiters to currBreak
    if (count currTxt's text items) > 1 then
        set currList to my (textItems from currTxt)
        repeat with n from 2 to count currList
            set my currList's item n to initialCap(my currList's item n)
        end repeat
        set text item delimiters to currBreak's contents
        tell my currList to set currTxt to beginning & ({""} & rest)
    end if
end repeat
currTxt
end capItems

on modItems from currTxt against modList
    set currList to modList
    set currCount to (count modList) div 2
    repeat with currBreak in everyBreak
        set text item delimiters to currBreak
        if (count currTxt's text items) > 1 then repeat with n from 1 to currCount
        set text item delimiters to my currList's item n & currBreak
        if (count currTxt's text items) > 1 then
            set currTxt to textItems from currTxt
            set text item delimiters to my currList's item -n & currBreak
            tell currTxt to set currTxt to beginning & ({""} & rest)
        end if
    end repeat
end repeat
currTxt
end modItems

to changeCase of currTxt to caseType
if (count currTxt's words) is 0 then return currTxt

ignoring case
    tell caseType to set {upper_Case, lower_Case, sentence_Case, capitalized_Case, title_Case} to {it is "upper", it is "lower", it is "sentence", it is "capitalized", it is "title"}
end ignoring

if not (upper_Case or lower_Case or capitalized_Case or sentence_Case or title_Case) then
    error "The term \"" & caseType & "\" is not a valid case type option. Please use \"upper\", \"lower\", \"sentence\", \"title\" or \"mixed\"."
    else if upper_Case then
    set n to 1
    else
    set n to -1
end if

considering case
    set tid to text item delimiters

    repeat with n from n to n * (count lowerStr) by n
        set text item delimiters to my alphaList's item n
        set currTxt to textItems from currTxt
        set text item delimiters to my alphaList's item -n
        tell currTxt to set currTxt to beginning & ({""} & rest)
    end repeat

    if sentence_Case then
        set currTxt to initialCap(modItems from (capItems from currTxt against sentenceBreak) against sentenceModList)
        else if capitalized_Case or title_Case then
        set currTxt to initialCap(capItems from currTxt against whiteSpace)
        if title_Case then set currTxt to initialCap(capItems from (modItems from currTxt against mixedModList) against sentenceBreak)
    end if

    set text item delimiters to tid
end considering
currTxt
end changeCase

end script
tdnelson2

根据以上张贴者的建议,我能够NSString uppercaseString用来解决此问题:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set aPhrase to "this is 'a' test phrase with 'punctuation'"
its makeTitleCase(aPhrase)

on makeTitleCase(stringOfText)
    set makeLowerCaseList to {"A", "For", "So", "An", "In", "The", "And", "Nor", "To", "At", "Of", "Up", "But", "On", "Yet", "By", "Or"}
    set theString to current application's NSString's stringWithString:stringOfText
    set theString to (theString's capitalizedString())
    set stringOfText to theString as text
    set AppleScript's text item delimiters to " "
    set listOfWords to text items of stringOfText
    set listOfWords to listOfWords as list
    repeat with x from 2 to count of listOfWords -- item 2 because per APA Style the first word is capitalized irregardless
        set targetItem to (item x of listOfWords)
        set aString to (" " & targetItem & " ")
        set theWord to words of aString as list
        repeat with y from 1 to count of makeLowerCaseList
            set origWord to (item 1 of theWord) as text
            set compareWord to (item y of makeLowerCaseList) as text
            if origWord = compareWord then
                set oneWord to (current application's NSString's stringWithString:(item x of listOfWords))
                set item x of listOfWords to (oneWord's lowercaseString) as text
            end if
        end repeat
    end repeat
    return listOfWords as text
end makeTitleCase


on makeALLCAPS(stringOfText)
    set theString to current application's NSString's stringWithString:stringOfText
    set theString to (theString's uppercaseString())
    set stringOfText to theString as text
    return stringOfText
end makeALLCAPS

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 AppleScriptObjC 中使用 Xcode 中的段落

来自分类Dev

如何使用AppleScriptObjC和NSPasteboard读取剪贴板

来自分类Dev

使用Xcode将bash shell脚本嵌入到AppleScriptObjC应用程序中

来自分类Dev

AppleScriptObjC中的NSImage release()

来自分类Dev

在AppleScriptObjC中解析NSAppleEventDescriptor

来自分类Dev

ApplescriptObjC文件夹路径

来自分类Dev

带有ARC的AppleScriptObjc中的@autoreleasepool

来自分类Dev

如何获取ApplescriptObjC中滑块的值?

来自分类Dev

支持共享AppleScriptObjC变量以标记对象

来自分类Dev

如何在AppleScriptObjC中创建NSRange对象

来自分类Dev

如何在ApplescriptObjC中获取复选框的值

来自分类Dev

ApplescriptObjC-在标签上显示文本字段文本

来自分类Dev

在xcode项目中使用Matlab功能

来自分类Dev

使用继承在Codeigniter项目中进行设计时遇到麻烦

来自分类Dev

在python中使用beautifulsoup遇到麻烦

来自分类Dev

在Swift中使用多节TableView遇到麻烦

来自分类Dev

在xtensor中使用xt :: where遇到麻烦

来自分类Dev

我在Excel中使用宏遇到麻烦

来自分类Dev

在Python中使用str(count)遇到麻烦

来自分类Dev

在Xcode 5项目中使用Core Plot 2

来自分类Dev

在Xcode 12的SwiftUI项目中使用ViewRouter

来自分类Dev

在Xcode Swift项目中使用Objective C框架

来自分类Dev

在Xcode 5项目中使用Core Plot 2

来自分类Dev

在 Codepen 项目中使用 fullpage.js 时遇到问题

来自分类Dev

在项目中使用包

来自分类Dev

我在使用Android Studio项目时遇到了麻烦

来自分类Dev

我在使用Android Studio项目时遇到了麻烦

来自分类Dev

在if语句中使用布尔方法遇到麻烦

来自分类Dev

在Django中使用自定义http标题遇到麻烦

Related 相关文章

热门标签

归档