首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OS拖动URL / weblog生成网页PDF

OS拖动URL / weblog生成网页PDF
EN

Stack Overflow用户
提问于 2016-12-27 19:04:05
回答 1查看 531关注 0票数 0

我经常保存网页供将来使用,并希望尝试简化这一过程。

说我打开了狩猎和探测仪。Finder包含我的当前项目。我浏览网页研究并找到一些感兴趣的东西,然后我把网页地址从safari拖到finder --这将创建一个.webloc链接。

这是不理想的,有几个原因;

  1. .webloc文件不适用于windows客户端
  2. 如果网站因任何原因被删除或更改,我的链接本质上就失效了。

理想情况下,我想要做的是将网站捕获为PDF格式。我知道我可以选择适当的菜单选项导出为PDF,或打印/保存为PDF等。但是它中断了我的工作流程,因为它涉及更多的步骤,而且还必须指定要导出到每个时间的文件夹。

我认为Automator / Apple脚本可能会有所帮助,下面的伪代码是这样的:

代码语言:javascript
复制
If finder has item dropped on it
    If the item is a URL or .Webloc
        Use Safari’s built in functionality to generate a PDF from the URL in the folder the item was dropped on
    end if
end if

然而,我确实发现我与AppleScript斗争,所以有人能告诉我,如果这是可能的,甚至在我潜入之前?

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-12-28 11:27:36

最好的方式来做你想做的事情,所以混合一个自动化服务和一个AppleScript。

Automator服务将只包含一个操作“receive”,并将其设置为接收来自Safari的选择文本。AppleScript的操作如下:

代码语言:javascript
复制
on run {input, parameters}

set myPath to "/Users/your_User/Desktop/Test_URL" -- set your default saving path
tell application "Safari"
activate
set myURL to URL of front document
set myURL to my CleanName(myURL)
tell application "System Events" to tell application process "Safari"
    click menu item 15 of menu 3 of menu bar 1 -- export as PDF (Safari 10.0.2 / El Capitain)
    delay 0.5
    keystroke myURL & ".PDF"
    delay 0.2
    keystroke "g" using {command down, shift down} -- go to dialog
    keystroke myPath -- path to saving folder
    delay 0.2
    keystroke return -- close go to dialog
    delay 0.2
    keystroke return -- close the save as dialog
end tell
end tell
return input
end run

on CleanName(S) --replace special characters not allowed for file name with _
set AppleScript's text item delimiters to {":", "/", ","}
set SL to text items of S
set AppleScript's text item delimiters to {"_"}
return SL as text
end CleanName   

这个脚本运行模拟使用Safari菜单导出作为PDF。如果使用不同版本的Safari,菜单项可能与我的不同。请调整一下。

我假设PDF文件应该基于URL值(最后是+ PDF !)。为此,我还添加了一个子例程来清理URL,将其转换为OK文件名。我将/:这样的所有特殊字符替换为char _。

变量myPath包含到默认保存位置的路径:必须用用户名更改“your_user”,并确保桌面上存在文件夹TEST_URL。该脚本包含许多注释,以帮助您根据特定需求进行调整。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41350756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档