AppleScript删除两个字符串之间的所有文本 [英] Applescript to remove all text not between two strings

查看:337
本文介绍了AppleScript删除两个字符串之间的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图制作一个脚本,以显示当前iTunes前20名的对话框.我打算如何执行此操作,是从前100名网站获取html代码,然后提取两个字符串之间的文本以获取名称这首歌.对于第一首歌,这是非常成功的.

I'm trying to make a script that displays a dialog of the current iTunes Top 20. How I intend to do this is get the html code from the top 100 website and then extract for text between two strings to get the name of the song. For the first song, this is extremely successful.

但是,每次仅适用于第一首歌曲.我想解决此问题的唯一方法是,然后获取两个字符串之间的所有内容,我可以删除它们之间的所有 not .希望这会给我所有歌曲名称作为字符串.

However, it only works for the first song each time. The only way I can think of to fix this is rather then get everything between the two strings, I could delete everything not between them. This would hopefully then give me all the song names as a string.

有人知道该怎么做吗?

获取HTML:

set curlcommand to "curl https://www.apple.com/itunes/charts/songs/"
set html to (do shell script curlcommand) 

获取歌曲名称:

set AppleScript's text item delimiters to "width=\"100\" height=\"100\" alt=\""
set theText to item 2 of every text item of html
set AppleScript's text item delimiters to "\"></a>"
set theText to item 1 of every text item of theText
set AppleScript's text item delimiters to ""

推荐答案

您可以使用shell逐行获取所有歌曲名称的列表(这不是AppleScript列表).

You could use the shell to get a list with all song names line by line (this is not a AppleScript list).

set charts to (do shell script "
curl -s 'https://www.apple.com/itunes/charts/songs/'| tr '\"' '\n' |awk '/^ alt=$/ {getline;print}'")

tr '\"' '\n'用新行替换双引号(tr手册页)

tr '\"' '\n' substitutes the double quotes with new lines (tr manual page)

awk '/^ alt=$/ {getline;print}'alt=行之后打印写有歌曲名称的行( awk手册页面)

awk '/^ alt=$/ {getline;print}' prints the line after the line alt= where the song name is written (awk manual page)

这篇关于AppleScript删除两个字符串之间的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆