Can you create a go application with drag/drop functionality?

agolangf · · 862 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Sorry for the poorly worded question. Photoshop use to have the ability to create droplets, where you configured a batch process and it produced an icon for users that allowed you to drop a folder or a large selection of files on and it executes the process against each item being dropped. </p> <p>I&#39;d like to do a similar thing, allowing users to drop a csv file/folder of csv files... do some work on them, and then export the results. Without a GUI or anything. </p> <p>How would I go about that. Or I guess more accurately, when you drag and drop a file onto a built go executable does that count as the first parameter as if you were running the program from the commandline?</p> <hr/>**评论:**<br/><br/>porkbonk: <pre><p>As long as you don&#39;t need an actual GUI, you can simply drop files onto the executable.</p> <p>The file paths will end up in <code>os.Args</code>. The first string is the application itself.</p> <p>You could also use <code>flag.Parse()</code> and <code>flag.Args()</code> in which case the first string will already be removed. Definitely the better option if you&#39;re going to add flags anyway (like <em>optional</em> size, output path etc.)</p></pre>Sythe2o0: <pre><p>I tested this with the following:</p> <pre><code>package main import ( &#34;io/ioutil&#34; &#34;os&#34; ) func main() { bytes := []byte{} for _, s := range os.Args { bytes = append(bytes, []byte(s)...) bytes = append(bytes, &#39;\n&#39;) } ioutil.WriteFile(&#34;out.txt&#34;, bytes, 777) } </code></pre> <p>And it produced, when I dragged in image onto it:</p> <pre><code>...\Desktop\dragAndDrop.exe ...\Desktop\input.PNG </code></pre> <p>So on windows, yes. I can&#39;t speak for other platforms.</p></pre>fallen77: <pre><p>Awesome, thank you so much for taking the time out to do that.</p></pre>colezlaw: <pre><p>On MacOS, it&#39;s not quite as simple. You end up having to create an Automator application to run a shell script with something like:</p> <pre><code>/path/to/drop $* </code></pre> <p>And then it&#39;s not really clear what the CWD is when that runs, so you may have to add a <code>cd</code> to the front of it or have your Go program deal with real paths (blech!).</p></pre>fallen77: <pre><p>Crap, that&#39;s good to know. Bit naive of me to thing those things would be standard. Heh. </p></pre>__crackers__: <pre><p>I <em>believe</em> Mac apps set the working directory to the Resources folder in the app bundle.</p></pre>ruralcoypu: <pre><blockquote> <p>when you drag and drop a file onto a built go executable does that count as the first parameter as if you were running the program from the commandline?</p> </blockquote> <p>Why don&#39;t you write a simple program that prints its arguments, and experiment?</p></pre>fallen77: <pre><p>I&#39;m on public transit. Figured I&#39;d ask, but fair enough. </p></pre>1Gijs: <pre><p>Maybe a bit more esoteric, but on OSX the launchd deamon can be used to setup the OS to watch a dir for new or changing files. The thing I like about launchd is you can (un)install everything without using a gui. See the very good description at <a href="http://www.launchd.info" rel="nofollow">www.launchd.info</a></p> <p>In this you would like to use WatchPaths option:</p> <p>If the path points to a file, creating, removing and writing to this file will be start the job.</p> <p>If the path points to a directory, creating and removing this directory, as well as creating, removing and writing files in this directory will start the job. Actions performed in subdirectories of this directory will not be detected.</p></pre>pinpinbo: <pre><p>Heh, time for me to shine on Reddit :p</p> <p>My home backup system is like ghetto version of Dropbox, I built it just for fun.</p> <p>I created a Go daemon using <a href="https://github.com/fsnotify/fsnotify" rel="nofollow">fsnotify</a>. It watches a certain directory, when it sees a new file, perform upload to S3 and delete the local copy after verifying the checksum on S3.</p> <p>So you could do the same thing, watch a certain directory, do some work when new files are dropped to a directory.</p></pre>fallen77: <pre><p>That sounds like a fun project to mess around with. Unfortunately I am looking for a way to make it super easy for end users to process files. Does your dropbox do any updated syncing? Could be an interesting next step. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

862 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传