Applescript检查用户输入的第一个字母

Applescript to check the first letter of user input

我很难知道从哪里开始。

我正在设置一个预制电子邮件,它将在邮件内容文本中填写文件位置。

我想做的是找出用户输入的代号的第一个字母,并将结果分配给一个变量。如果变量中的第一个字母是 A 那么它将在文件夹 A-D 中,如果它的 E 它将在 E-H 中。

然后在我的电子邮件中我会这样说:

代号可用: resources/images/web_images/A-D/Codename.jpg

Ecodename 可用: resources/images/web_images/E-H/ecodename.jpg

我可以获取用户输入并分配变量,但它会根据可能的位置检查第一个字母,然后将新位置分配给可在电子邮件中使用的变量。

这部分脚本是更大脚本的一部分。它所做的概述是:它在 photoshop 中打开一个 pdf,调整它的大小并将其命名为用户输入的代码,将文件保存到桌面并构建一个包含图像所在位置的电子邮件,如果它以 A-B 开头的话将位于特定文件夹中,E-G 位于不同的现有文件夹中。我会手动将图像放在那里,但我想检查输入的名称(代号),所以当我构建电子邮件时,我可以自动附加位置。

我相信这是一个 if else 但不确定。 感谢您的帮助。

我不完全相信我已经完全理解你的问题,但看到其他答案的方法和你反对它的评论,我希望我在球场上。

正如您所说,您知道如何获取用户输入并将其分配给变量,我没有将这些方面作为我下面脚本的一部分。

相反,我创建了一个虚拟 属性 值,用于存储我认为用户可能输入的代号的虚构值。

property userInput : "GXTrZy"

您可以删除此行并将我的脚本中出现的任何 userInput 替换为您用于存储实际用户输入的变量的名称。

property userInput : "Dinosaur" -- A dummy value to take the place of user input
-------------------------------------------------------------------------------------
property resourcePath : "resources/images/web_images"
property resourceFolders : ["A-B", "C-F", "G-I", "J-O", "P-S", "T-Z"]
-------------------------------------------------------------------------------------
property alphabet : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-------------------------------------------------------------------------------------
(*

    Your code to obtain user input
    and assign it to a variable, etc.

    I have put a dummy placeholder codename
    in the property value 'userInput' at
    the top of this script.

*)

set alpha to the first character of the userInput --> "D"
if alpha is not in the alphabet then return
set x to (alpha's id) mod 32 -- Alpha's ordinal position in the alphabet

set || to words of (resourceFolders as text) as text --> "ABCFGIJOPSTZ"
set the text item delimiters to {"/"} & ||'s items
--> {"/", "A", "B", "C", "F", "G", "I", "J", "O", "P", "S", "T", "Z"}

set |ₓ| to text 1 thru x of the alphabet --> "ABCD"
set |₋| to text items of ("+" & |ₓ|) as text --> "+///D"

set i to 0.49 + (offset of alpha in ||) / 2 as integer --> 0
set j to the (number of words in |₋|) * ((|₋| contains alpha) as integer) --> 2

set n to i + j --> Index of folder item in resourceFolders list = 2

set resourceFolder to item n of resourceFolders --> "C-F"

set filepath to the {resourcePath, resourceFolder, [userInput, ".jpg"]} as text
set msg to ["The codename you input : ", userInput, linefeed, ¬
    "The file for that codename is available at : ", filepath] as text

(*

    The rest of your code will follow
    below.  You can use the message stored
    in the variable 'msg' to append to 
    your email. 

*)

与您的问题相关的特别感兴趣的是这里的这一行:

set alpha to the first character of the userInput --> "D"

它获取代号的第一个字母并将其存储在变量alpha中。接下来的代码行处理识别包含代号资源文件的正确文件夹。

一些代码行之后的注释以---->开头,后者我用来在脚本执行的每个阶段打印结果基于我提供的示例用户输入。因此,正在处理的字母是 "G",显示的结果与该字母相关,但会因使用不同首字母的不同样本输入而有所不同。

在运行这个脚本之后,最终输出的是存储在变量msg中的消息字符串,其中包含以下文本:

"The codename you input : Dinosaur
The file for that codename is available at : resources/images/web_images/C-F/Dinosaur.jpg"

更新: 2018-09-26 - 一种迭代方法

这是实现相同目标的另一种方法,它使用 repeat 循环遍历每个可能的资源文件夹名称,并测试代号的字母是否位于文件夹名称的字母之间:

property userInput : "Dinosaur" -- A dummy value to take the place of user input
-------------------------------------------------------------------------------------
property resourceFolders : ["A-B", "C-F", "G-I", "J-O", "P-S", "T-Z"]
-------------------------------------------------------------------------------------
set alpha to the first character of the userInput --> "D"

set ASCIIindices to the id of (words in (resourceFolders as text) as text)
set alphaIndex to ((alpha's id) mod 32) + 64 -- Case independent

repeat with i from 2 to the length of ASCIIindices by 2
    if item i of ASCIIindices ≥ the alphaIndex then exit repeat
end repeat

set resourceFolder to item (i / 2) of the resourceFolders --> "C-F"

代码更短,实际上是解决此类问题的更明显的方法。但是,我倾向于在研究解决方案之前概括问题,这就是我上面更偏向于数学的解决方案的来源。

第一个解决方案的理论上的好处是它的执行速度仅受限于 AppleScript 的内置函数的速度,如 offset 和文本操作功能,即实际的 AppleScript 代码无论字母有多长,都不会减慢进程。

同时,迭代方法是一种 O(n) 算法,使用较长的字母表会变慢,因为它的速度限制因素是需要无限循环的 repeat 循环无限字母表的时间。

但是,鉴于我们没有无限的字母表,这确实是一个有争议的问题,因此您可以选择使用更短、更容易理解的方法。我个人不喜欢 repeat 循环并在必要时避免使用它们,但这完全是我在编码中的特殊特征。